Reputation: 5063
My idea is to remove special characters and html codes and replace the whitespaces to dashes let us doing it step by step
$text = "Hello world)<b> (*&^%$#@! it's me: and; love you.<p>";
now i'm want the output become Hello-world-its-me-and-love-you
I've tired this code for removal of special characters and html codes
$array = array();
$array[0] = "/<.+?>/";
$array[1] = "/[^a-zA-Z0-9 ]/";
$textout= preg_replace($array,"",$text);
Now the output will be like this Hello world its me and love you
so is there any way i can modify this code so that the text output become exact as i do needs Hello-world-its-me-and-love-you
~ thank you
Upvotes: 1
Views: 163
Reputation: 5475
You're probably better off using strip_tags
to get rid of the html tags for you, then use a regexp to remove all non alphanumeric (or non-space) characters. Then you can simply convert spaces to hyphens using str_replace
. Note I also added a line to collapse multiple spaces to a single space, since that is what you did in your example. Otherwise you get world--its-me
instead of world-its-me
.
<?php
$text = "Hello world)<b> (*&^%$#@! it's me: and; love you.<p>";
$text = strip_tags($text);
$text = preg_replace('/[^a-zA-Z0-9 ]/', '', $text);
//this is if you want to collapse multiple spaces to one
$text = str_replace (' ', ' ', $text);
$text = str_replace (' ', '-', $text);
?>
Upvotes: 2
Reputation: 8254
You could just add
$textout = str_replace(' ', '-', $textout);
after your last line to replace the spaces with hypens.
Upvotes: 1