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?
tag-url
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 -
seo-friendly-slug
Kindly use the slug() function while inserting all tags.
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.
<?php
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";
$tagarray=explode(",",$tags);
$newarr=array();
foreach($tagarray as $tag)
{
$tag=slug($tag);
$newarr[]=$tag;
}
echo $seo_friendly=implode(",", $newarr);
?>
Output-
seo-friendly,php-programming,my-slug
Use the slug() function to add minus - sign between spaces in the tag links -
Learn here -
https://technosmarter.com/qa/585/i-want-to-make-seo-friendly-url-in-php
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>
This live website is powered by Techno Smarter QA. The complete source code is now available with a powerful admin panel, responsive design, future updates, and support.