Forum Moderators: coopster

Message Too Old, No Replies

Is there a better way to convert a string

         

itledi

11:43 pm on Jan 13, 2008 (gmt 0)

10+ Year Member



I'm hoping theres a way to simplify my mess.

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?

PHP_Chimp

8:52 pm on Jan 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$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 dont know if its any easier, but preg_replace can take arrays for the pattern and replacements, so you can put then all together, as opposed to having a string of different statements.
The great thing about php is that there are so many different ways to do things. Although you say that yours look "overwhelming" it is easy to read through and see what is happening. To arrays may not so easy to 'read' through, but it does condense the code into 5 lines...when you get rid of all the comments ;)

itledi

3:12 pm on Jan 15, 2008 (gmt 0)

10+ Year Member



Thank you very much.

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?

coopster

12:29 am on Jan 16, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The percent sign is being used as the delimiter [php.net] character.

PHP_Chimp

5:50 pm on Jan 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You shouldnt need to ksort the arrays because (from the manual) -
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.

So both arrays are worked through in order...that is assuming I put the correct number of elements in each array.

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.