PHP ereg_replace 问题
几个 PHP ereg_replace 问题。我有一个名称数组:
$ssKeywords = array("Str", "Int", "String", "As", "Integer", "Variant");
但是,当我使用此 ereg_replace: 时,
foreach($arKeyword as $aWord) {
$sCode = ereg_replace($aWord, "<span class='ssKeyword'>".$aWord."</span>", $sCode);
}
它只会找到“str”或“int”而不是完全匹配。显然 ereg_replace 是贪婪的,那么为什么它不寻找完整的匹配项呢?
我设法使用 preg_replace 使注释正常工作。
A couple of PHP ereg_replace questions. I have an array of names:
$ssKeywords = array("Str", "Int", "String", "As", "Integer", "Variant");
However when I use this ereg_replace:
foreach($arKeyword as $aWord) {
$sCode = ereg_replace($aWord, "<span class='ssKeyword'>".$aWord."</span>", $sCode);
}
It will only find the "str" or "int" not the full match. Apparently ereg_replace is greedy, so why is it not looking for the full match?
I managed to get comments working using preg_replace.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你需要使用ereg吗?它已被弃用,并且从 PHP 6 开始将被废弃。我建议您使用 preg,它也更高效。
此信息可在 php.net/ereg 获取
Do you need to use ereg? it has been deprecated and will be obsolete as of PHP 6. I suggest you use preg, which is also more efficient.
This information is available at php.net/ereg
与其一次搜索一个术语,不如一次搜索所有术语:
如果按长度对术语进行排序,您会发现 Integer 而不是 Int >:
Instead of searching for one term at a time better search for all of them at a time:
And if you sort the terms by its length, you will find Integer instead of just Int:
您的问题与 ereg_replace 无关。 preg_replace 不仅是一个更好的选择,而且实际上在您的情况下您没有使用任何类型的正则表达式;你只是在做一个简单的字符串匹配。因此 str_replace 会更快、更清晰。
问题是你正在做:
它从数组的第一个元素循环到最后一个元素,按照你声明它们的顺序针对每个关键字测试整个字符串。您首先声明了“Int”,因此在循环到达“Integer”关键字之前,字符串中的任何“Integer”都将被“
更改数组顺序,使较长的关键字排在前面,这样就可以了。
Your problem is nothing to do with ereg_replace. Not only is preg_replace a better bet, but in fact in your case you aren't using any type of regular expressions; you're just doing a plain string match. So str_replace would be quicker and clearer.
The problem is that you're doing:
which loops from the first to the last element of the array, testing the whole string against each of the keywords in the order you declared them. You declared ‘Int’ first, so any ‘Integer’ in the string will get replaced by “<span class="ssKeyword">Int</span>eger” before the loop gets as far as the ‘Integer’ keyword. By which time, with the “</span>” in the way, it'll never match.
Change the array order so that the longer keywords come first and it'll work.
如果您正在进行纯文本匹配,那么
str_replace
会更高效且不必要。如果您将来确实需要正则表达式,请使用preg_*
函数,因为ereg_*
已弃用,而preg_*
速度更快。关于您关于“贪婪”的问题,这是指您实际使用正则表达式的时间。例如,如果您有文本:
并使用这样的正则表达式:
那么它将匹配整个字符串,因为 + 运算符是贪婪的,并且会在一行上找到尽可能多的内容。您需要这样做以阻止它贪婪并匹配每个短语:
If you're doing a plain-text match then
str_replace
is more efficient and less unnecessary. If you do need regular expressions any time in the future, use thepreg_*
functions becauseereg_*
is deprecated andpreg_*
is faster.With regard to your question about "greedy", that refers to when you're actually using regular expressions. For example if you have the text:
And use a regex like this:
Then it will match the entire string because the + operator is greedy and finds as much as it can on one line. You'd need to do this to stop it being greedy and match each of the phrases: