Forum Moderators: coopster

Message Too Old, No Replies

more than one occurrence

         

FiRe

12:54 pm on Sep 30, 2006 (gmt 0)

10+ Year Member



I need a function to replace this:

This_is__a_test

=

This_is_a_test

Another example:

My__Name____Is_Joe

=

My_Name_Is_Joe

So it has to filter out more than 1 occurrence of _ if followed by another _
Can anyone help?

Birdman

3:17 pm on Sep 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php
$str = 'foo__o';
$str = preg_replace('/__+/', '_', $str);
// This will be 'foo_o' now
echo $str;
?>

Or. in case you wanted to strip extra whitespace:

<?php
$str = 'foo o';
$str = preg_replace('/\s\s+/', ' ', $str);
// This will be 'foo o' now
echo $str;
?>

This info came from the PHP manual, on the preg_replace page.

php.net/manual/en/function.preg-replace.php