Forum Moderators: coopster
What I'm trying to do seems simple enough. To change:
" This
Statement."
into:
"this-statement"
Specificly what I'm trying to do is to take a string of text and remove all the line breaks and convert them to hypens. I also need to convert to lowercase, remove all nonalphanumeric or hypen characters, convert spaces to hypens, to strip hypens from the beginning and end of the string, and make sure there aren't more than one hypen in a row.
$string=urldecode($string);
$string=str_replace(array("\r","\n")," ",$string));
$string=trim($string);
$string=strtolower($string);
$string=preg_replace("[^a-z0-9\s]","",$string);
$string=str_replace(" ","-",$string);
echo $string;
Not only does it look overwhelming, but its slipping errors. Sometimes it seems to get stuck, and I also caught things like question marks being outputed.
Is there a better way?
$pattern = array(
'%\n%', /* remove all the line breaks and convert them to hypens */
'%[^a-z0-9-]%i', /* remove all nonalphanumeric or hypen characters */
'% %', /* convert spaces to hypens */
'%^-+(.*)-+$%', /* strip hypens from the beginning and end of the string */
'%(-)+%' /* make sure there aren't more than one hypen in a row. */
);
$replace = array('-', '', '-', '$1', '$1');
$string = preg_replace($pattern, $replace, $string);
$string = strtolower($string);
echo $string
I also found out that it will be important to do ksort to both the arrays before the preg_replace.
$pattern=array('%\n%','%[^a-z0-9-]%i','% %','%^-+(.*)-+$%','%(-)+%');
$replace = array('-','','-','$1','$1');
ksort($pattern);
ksort($replace);
$string = preg_replace($pattern, $replace, $string);
$string = strtolower($string);
echo $string
May I ask what the percent sign means in the regular expression?
The string or an array with strings to replace. If this parameter is a string and the pattern parameter is an array, all patterns will be replaced by that string. If both pattern and replacement parameters are arrays, each pattern will be replaced by the replacement counterpart. If there are fewer elements in the replacement array than in the pattern array, any extra pattern s will be replaced by an empty string.
I tend to use % as the delimiter as you dont often find them in the strings that you are using. A lot of people use / but as / appears a LOT when dealing with url's you see lots of expressions with \/ all over the place. I try to avoid having to escape lots of characters so % works well as the delimiter...but its all personal preference.