Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP str_replace any number pattern

I have what seems like quite a simple query. I have the following code in PHP:

$newThumb = str_replace('style="width:170px;"','',$nolinkThumb);

The problem that I have is that the number '170' can be any number therefore I would like my str_replace to reflect this. I have tried using:

$newThumb = str_replace('style="width:[0-9]px;"','',$nolinkThumb);

But this doesn't work. Any help would be greatly appreciated.

Thank you

like image 989
David Brooks Avatar asked Aug 04 '26 23:08

David Brooks


1 Answers

You need to use regex, but [0-9] in regex means "the digits 0 to 9 repeated once".

What you're actually searching for is [0-9]+ which means "the digits 0 to 9 repeated one or more times":

$string = preg_replace('/style="width:[0-9]+px;"/', '', $string);
like image 195
h2ooooooo Avatar answered Aug 06 '26 14:08

h2ooooooo