PHP str_replace 任何数字模式 [英] PHP str_replace any number pattern
本文介绍了PHP str_replace 任何数字模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来很简单的查询.我在 PHP 中有以下代码:
I have what seems like quite a simple query. I have the following code in PHP:
$newThumb = str_replace('style="width:170px;"','',$nolinkThumb);
我遇到的问题是数字170"可以是任何数字,因此我希望我的 str_replace 能够反映这一点.我试过使用:
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.
谢谢
推荐答案
需要使用正则表达式,但是[0-9]
在正则表达式中的意思是数字0到9重复一次".
You need to use regex, but [0-9]
in regex means "the digits 0 to 9 repeated once".
您实际搜索的是 [0-9]+
,意思是数字 0 到 9 重复一次或多次":
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);
这篇关于PHP str_replace 任何数字模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文