Please Login or Register to continue.


I want to put ( - ) between spaces in tag links. I am creating tags in PHP. I want to create SEO-friendly tags with minus - sign between two words using PHP. 

`echo '<p>Tagged as: '; $links = array();

$parts = explode(',', $row['post_tag']);

foreach ($parts as $tags) {

$links[] = "<a href='".$tags."'>".$tags."</a>";

echo implode(", ", $links); echo '</p>';`

It's like this now

tag url

How do I put - between spaces?


(230 Points)
in PHP


3 Answers
(3.4K Points)

You can use the slug() function to create SEO-friendly tags. The slug() function will replace space with minus - sign.

For example - 

SEO friendly Slug 

Output - 


Kindly use the slug() function while inserting all tags. 

How to add minus - sign between tag words? 

We use explode function and create an array of all tags. Use a comma delimiter to make string to array - 

for example - 

Tags - seo friendly,php programming, my slug

First of all , convert this string to an array. 

Use foreach loop and slug function. 

Implode array to string.

function slug($text){ 
  $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
  $text = trim($text, '-');
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
$text = strtolower($text);
$text = preg_replace('~[^-\w]+~', '', $text);
if (empty($text))
return 'n-a';
return $text;
$tags="seo friendly,php programming,my slug"; 
foreach($tagarray as $tag)
echo $seo_friendly=implode(",", $newarr); 





(9.4K Points)

Use the slug() function to add minus - sign between spaces in the tag links - 

Learn here -



(230 Points)

Thank you very much for your reply
The negative sign is also placed in the title of the tag, I want it to be placed only in the link like WordPress tags.
Example: <a href="my-slug">my slug</a>

Why don't you remove the negative - sign from tag? <?php function remove_negative($tag){ $simple= preg_replace('[-]', ' ', $tag); return $simple; } $tag="my-slug"; echo remove_negative($tag); ?> Output my slug
vishalrana 03 Feb 2023

Related Questions :-

Featured Items:-

Certificate system in PHP website | PHP scripts


Home Credito loan finance management multipurpose system in PHP


Result management system with Marksheet in PHP website | PHP Scripts


Your Answer

You can post your answer after login..