PHP,删除已知字符串的更好选择是什么?

发布于 2024-09-05 18:38:14 字数 535 浏览 1 评论 0原文

我正在寻找并替换另一个字符串中的已知字符串。我应该使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒几许 2024-09-12 18:38:14

如果您的字符串是固定的,并且不需要正则表达式,请始终使用 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.

青春如此纠结 2024-09-12 18:38:14

如果您知道字符串并且不需要正则表达式,请使用 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...

¢好甜 2024-09-12 18:38:14

从 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文