How to make preg_replace() more greedy?
Posted: Fri Oct 02, 2009 12:58 pm
Hi
I am using the PHP Regex function preg_replace(), and I am having an issue that it is not greedy enough.
The result is "/stevel" an extra "l", instead of as desired "/steve". This is because preg_replace() used the first match "htm" to replace.
To make this work, I had to reverse order by length the strings within the $extensions_array array
The result is now correct, $result == "/steve"
Could I have done this differently? Is there another parameter that I could have added to $extensions_pattern, in addition to case-insensitive i to make the preg_replace more greedy?
Thanks
Jeff in Seattle
I am using the PHP Regex function preg_replace(), and I am having an issue that it is not greedy enough.
Code: Select all
$extensions_array = array( 'htm', 'html', 'php', 'asp', 'aspx' );
/* begin Regex Code */
$extensions_str = trim(implode("|", $extensions)); // "htm|html|php|..."
$extensions_pattern = "/.(".$extensions_str.")/i";
$value = "/steve.html";
$result = preg_replace($extensions_pattern, "", $value);
/* end Regex Code */
To make this work, I had to reverse order by length the strings within the $extensions_array array
Code: Select all
$extensions_array = array( 'htm', 'html', 'php', 'asp', 'aspx' );
/* Reverse order by length using functor */
uasort($extensions_array, "string_reverse_sort_by_length_functor"); // new order array( 'html', 'aspx', 'htm', 'php', 'asp' );
/*... same Regex Code as above ... */
/* Reverse order functor */
function string_reverse_sort_by_length_functor($val_1, $val_2)
{
$retVal = 0;
$firstVal = strlen($val_1);
$secondVal = strlen($val_2);
if($firstVal < $secondVal)
{ $retVal = 1; }
else if($firstVal > $secondVal)
{ $retVal = -1; }
return $retVal;
}Could I have done this differently? Is there another parameter that I could have added to $extensions_pattern, in addition to case-insensitive i to make the preg_replace more greedy?
Thanks
Jeff in Seattle