mb_eregi_replace 多个匹配项获取它们
$string = 'test check one two test3';
$result = mb_eregi_replace ( 'test|test2|test3' , '<$1>' ,$string ,'i');
echo $result;
这应该提供:
是否可以在不使用其他匹配函数的情况下找到test和test3?
$string = 'test check one two test3';
$result = mb_eregi_replace ( 'test|test2|test3' , '<$1>' ,$string ,'i');
echo $result;
This should deliver: <test> check one two <test3>
Is it possible to get, that test and test3 was found, without using another match function ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您的字符串是 utf-8,您可以使用 preg_replace 代替
显然,使用此类数据来匹配结果将不是最佳的
您将需要比直接搜索和替换为正则表达式更长的方法(当然,如果您的模式是前缀其他图案)
If your string is utf-8, you could use preg_replace instead
Oviously with this kind of data to match the result will be suboptimal
You'll need a longer approach than a direct search and replace with regular expressions (surely if your patterns are prefixes of other patterns)
首先,您想要增强的代码似乎不符合其最初的目的(至少在我的计算机中不符合)。您可以尝试这样的操作:
我删除了
i
标志(这当然在这里没有什么意义)。尽管如此,您仍然需要使表达式变得贪婪。至于最初的问题,这里有一些概念证明:
请注意,这段代码很糟糕并且可能不安全。老实说,我会选择 Gumbo 提出的
preg_replace_callback()
解决方案。To begin with, the code you want to enhance does not seem to comply with its initial purpose (not at least in my computer). You can try something like this:
I've removed the
i
flag (which of course makes little sense here). Still, you'd still need to make the expression greedy.As for the original question, here's a little proof of concept:
Please note this code is horrible and potentially insecure. I'd honestly go with the
preg_replace_callback()
solution proposed by Gumbo.您可以使用
preg_replace_callback
代替:这里
preg_replace_callback
将调用为模式的每个匹配传递回调函数(请注意 其语法不同于POSIX)。在这种情况下,回调函数是一个 匿名函数,它将匹配添加到$matches 数组并返回要替换匹配项的替换字符串。
另一种方法是使用
preg_split
在匹配的分隔符处分割字符串,同时还捕获分隔符:结果是交替的不匹配部分和匹配部分的数组。
You can use
preg_replace_callback
instead:Here
preg_replace_callback
will call the passed callback function for each match of the pattern (note that its syntax differs from POSIX). In this case the callback function is an anonymous function that adds the match to the$matches
array and returns the substitution string that the matches are to be replaced by.Another approach would be to use
preg_split
to split the string at the matched delimiters while also capturing the delimiters:The result is an array of alternating non-matching and matching parts.
据我所知, eregi 已被弃用。
您可以执行以下操作:
这也允许您轻松添加更多字符串进行替换。
As far as I know, eregi is deprecated.
You could do something like this:
This too allows you to easily add more strings to replace.
您好,以下函数用于从字符串中查找任何单词
Hi following function used to found the any word from string