PHP,删除已知字符串的更好选择是什么?
我正在寻找并替换另一个字符串中的已知字符串。我应该使用 str_replace() 还是 preg_replace()?要替换的字符串类似于 [+qStr+]
、[+bqID+]
或 [+aID+]
,它是在与此类似的代码块中进行搜索:
<li> [+qStr+]
<ol class="mcAlpha">
<li><input type="radio" name="[+bqID+]" id="[+bqID+]_[+aID+]" value="[+aID+]" /><label for="[+bqID+]_[+aID+]">[+aStr+]</label></li>
</ol>
</li>
我将用 MySQL 查询的结果替换字符串,并一次执行此操作或类似操作最多 200 次。哪个函数 str_replace() 或 preg_replace() 是最简单和/或最快的方法。
I am looking to search for and replace a known string from within another string. Should I use str_replace() or preg_replace()? The string to be replaced would be something similar to [+qStr+]
, [+bqID+]
, or [+aID+]
and it would be being searched for in a code chunk similar to this:
<li> [+qStr+]
<ol class="mcAlpha">
<li><input type="radio" name="[+bqID+]" id="[+bqID+]_[+aID+]" value="[+aID+]" /><label for="[+bqID+]_[+aID+]">[+aStr+]</label></li>
</ol>
</li>
I would be replacing the strings with the results from a MySQL query, and be performing this action or similar up to 200 times at a time. Which function str_replace() or preg_replace() would be the easiest and/or quickest method to take.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的字符串是固定的,并且不需要正则表达式,请始终使用 str_replace,因为它会更快。此外请注意,您应该忘记 ereg_replace,并始终使用 preg_replace,因为前者已被弃用。
If your string is fixed, and you don't need regular expressions, always use str_replace, as it will be faster. Note moreover that you should forget about ereg_replace, and always use preg_replace, as the former has been deprecated.
如果您知道字符串并且不需要正则表达式,请使用 str_replace()。它更快,因为如果您使用正则表达式,则不需要尝试。
PS:对于正则表达式,你应该使用 preg_replace() 而不是 ereg_replace(),只是为了将来......
if you know the string and don't need regular expressions, use str_replace(). it's faster because it doesn't need to try if you use a regex.
PS: for regex you should use preg_replace() instead of ereg_replace(), just for the future...
从 PHP 5.3.0 开始,ereg_replace() 已被弃用,所以我不建议使用它。
str_replace() 或 preg_replace() 将是合理的选择。
就我个人而言,我会使用 str_replace() 进行简单的搜索/替换; preg_replace 用于比简单匹配更复杂的任何内容
Well ereg_replace() is deprecated as of PHP 5.3.0, so I wouldn't advise using it.
str_replace() or preg_replace() would be the logical alternatives.
Personally I'd use str_replace() for a simple search/replace; preg_replace for anything more complex than simple matches