Hi,
I have a string that is
$str = "testingSUB1";
How can I strip out SUB* from the string? I assume using preg_replace but I'm not good with matching what I want with regex.
Does anyone know how I can do this?
Thank you
Hi,
I have a string that is
$str = "testingSUB1";
How can I strip out SUB* from the string? I assume using preg_replace but I'm not good with matching what I want with regex.
Does anyone know how I can do this?
Thank you
That's the way.
$str = preg_replace("#SUB[0-9]+#", "", $str);
The #s are delimiters; they can be any non-alphanumeric/whitespace/backslash character that doesn't appear in the pattern. [0-9] means any digit (you can use \d too in some languages, but I usually don't bother), and the + means one or more of the previous, so if you take the + out it will only replace the first digit
This should do it:
$word = 'SUB';
$string = 'testingSUB1';
echo preg_replace('~^(.*?)(' . preg_quote($word, '~') . '\d+)(.*?)$~', '$1$2', $string);
EDIT - This is better:
echo preg_replace('~' . preg_quote($word, '~') . '\d+~', '', $string);