Is there a better way than this to replace a chracter in a string?

I am working on my url to make it beautiful. and here is the logic to which I have come.

now in the url I want to achieve something similar.

http://domain.com/category/date/post-title

for this, I first populated the value from the database, that is, the date and title, like this

for date:

$date = date("d", $row['timestamp']);
$month = date("m", $row['timestamp']);
$year = date("Y", $row['timestamp']);
$date_url = $date.$month.$year;

for title:

$title = $row['title'];
$title_url = str_replace(" ", "-", $title);

I have now created a hyperlink to send it to a URL like this.

<a href="news.php?id=<?php echo $id; ?>&cat=<?php echo 'news'; ?>&date=<?php echo $date_url; ?>&title=<?php echo $title_url; ?>"><img src="<?php echo 'admin-login/'.$pic_title; ?>"/></a>

My main problem is the header, which I fill with the value from the database, is it normal to use str_replace()for this? or is there a better way?

Am I mistaken anywhere, or is it good to continue this logic?

thank..

+3
source share
4

char.

:

$from = array('/\W/','/-+/','/^-/','/-$/');
$to = array('-','-','','');
$title = preg_replace($from,$to,$title);

char -, - -, -, .

+2

:

$date = date("dmy", $row['timestamp'])
preg_replace
$title = strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), $title));
+2
$title = preg_replace('/\W/', '-', $row['title']);
+1

sanitize , , .

0

Source: https://habr.com/ru/post/1762797/


All Articles