×

Please Login or Register to continue.

2
(616 Views)

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

(230 Points)
in PHP

Share

3 Answers
(3.4K Points)
(edited)
2

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

 


Comment

(9.4K Points)
2

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


Comment

(230 Points)
0

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>


Comment
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

$22



Home Credito loan finance management multipurpose system in PHP

$65



Result management system with Marksheet in PHP website | PHP Scripts

$39




Your Answer

You can post your answer after login..