Logo

1.3K views Asked 01 Feb 2023 in PHP
2

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

User moskano (230 Points)

3 Answers
2
vishalrana (3.5K Points)

answered 01 Feb 2023 (edited)

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. 

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.

<?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

 


2
shakti (9.5K Points)

answered 01 Feb 2023

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


0
moskano (230 Points)

answered 03 Feb 2023

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
Add comment

🚀 Build Your Own Q&A Community Website

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.

Admin Panel Responsive SEO Friendly PHP
Get Source Code

Your Answer
×
Login Required

You must login to continue.

Login Login with Google
Register