php 相当于 preg_match 但不需要正则表达式的是什么?
在 PHP 中,是否有相当于 preg_match
且不需要使用正则表达式的方法? preg_replace
有 str_replace()
。有 preg_match
的东西吗?
*更新* 我只想用另一个字符串替换已知字符串。使用正则表达式似乎有点矫枉过正。
我有字符串“这是一个 [test1],而不是 [test2]”,我想将它们与“[test1]”和“[test2]”匹配。
In PHP is there an equivalent to preg_match
that does not require the use of regex? There is str_replace()
for preg_replace
. Is there something for preg_match
.
*update * I am only looking to replace a known string with another. Using regex just seems like overkill.
I have the string "This is a [test1], and not a [test2]" and I want to match them with "[test1]" and "[test2]".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您的意思是在不使用正则表达式的情况下在另一个字符串中查找一个字符串,则可以使用 strpos
If you mean find a string within another string without using regex, you can use strpos
为了回答你的问题,PHP 有一些没有正则表达式的函数
但它们根本无法完全取代
preg_match
To answer your question there is some function of PHP without regex
But they can not replace
preg_match
completely at all因为我不确定您正在寻找什么结果,所以我不能说这是否正是您正在寻找的结果。
您可以使用
strpos
来查看是否一个字符串在另一个字符串中出现。Since I am not sure what result you are looking for I can't say if this is exactly what you are looking for.
You can use
strpos
to see if an occurrence of one string is in another.首先,
str_replace()
不是preg_replace()
的替代品。函数str_replace()
用替换字符串替换所有出现的搜索字符串,preg_replace() 替换正则表达式选择的内容(这不是同一件事)。很多东西都需要正则表达式(这很好),所以你不能简单地用单个 PHP 函数替换它。
First,
str_replace()
is not replacement forpreg_replace()
. Functionstr_replace()
replaces all occurrences of the search string with the replacement string, preg_replace() replaces content selected by regular expressions (that's not same thing).A lot of things require regex (and that's good) so you can't simply replace it with single PHP function.
大多数开发人员使用 preg_match 是因为他们想要使用匹配项(将由函数设置的第三个参数)。
我想不出一个函数可以像匹配一样返回或设置相同的信息。
但是,如果您使用不带正则表达式的 preg_match 那么您可能不太关心匹配。
如果您使用 preg_match 来查看是否存在“匹配”,那么我建议使用 strpos 来代替,因为它可以更有效地查看一个字符串是否在另一个字符串中找到。
Most developers use preg_match because they want to use the matches (the third parameter which will get set by the function).
I can not think of a function that will return or set the same information, as done with matches.
If however, you are using preg_match without regex then you might not care as much about the matches.
If you are using preg_match to see if there is a "match" and just that then I'd suggest using strpos instead, since it is much more efficient at seeing if one string is found in another.