PHP 和正则表达式(如果找到单词)
我希望仅在字符串中找到某个单词时才执行一条字符带,因此如果单词编号与正则表达式匹配,它将执行一条带所有实际数字 0-9 的操作,或者 preg 将它们替换为空,顺便说一句数字总是用“”括起来。将这两个功能组合在一起的最佳方法是什么?一个例子是,如果数据是人,数字很有趣! “123ABC”它会返回伙计,数字很有趣! “ABC” 如果数字不存在,它们将被忽略。
I'm looking to only perform a strip of characters if a certain word is found in a string, so if the word numbers is matched with regex it will perform a strip all actual numbers 0-9 or preg replace them to nothing, BTW The numbers will always be wrapped in "". What would be the best way to put these two functions together? An example would be if the data was Man, Numbers are fun! "123ABC" it would return Man, Numbers are fun! "ABC" If numbers isn't present they are ignored.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我觉得这里的一些答案过于复杂。也许只是我,但这应该是您所需要的:
编辑:如果您只想要引号内的数字,您也许可以使用正则表达式来完成,但我肯定会这样做是这样的:
I feel like some of the answers here are overcomplicated. Maybe it's just me, but this should be all you need:
EDIT: If you only want numbers that are inside quotation marks, you might be able to do it with a regex, but I'd definitely do it this way:
如果我正确理解这个问题,也许是这样的:
If I understand the question correctly, maybe something like:
像这样的东西应该适合你:
\bNumbers\b 将确保将单词
Numbers
与单词边界匹配,以便xyzNumbers
和Numbersxyz
是 <强>不匹配。Something like this should work for you:
\bNumbers\b will make sure to match word
Numbers
with word boundaries so thatxyzNumbers
andNumbersxyz
are NOT matched.您可以首先使用 strpos 函数来检查字符串。
类似于
if(strpos($mystr,"Some value")!==false)
{
/*preg_replace在这里。*/
}
You could use the strpos function to check for the string in the first place.
Something like
if(strpos($mystr,"Some value")!==false)
{
/*preg_replace here.*/
}
@Ryan Cooper:尝试——
@Ryan Cooper: Try --