preg_replace 完全相反
我想做一些类似从这一行获得相反匹配的事情:
$input = "21.asdf*234true;asdf0--11"
$_BOOL_ ="/(true|false)/i";
$output = preg_replace($_BOOL_, '', $input);
//Result: "21.asdf*234;asdf0--11"
//Desired result: "true"
Which of course in php 5.3 is
$output = preg_filter($_BOOL_, '', $input);
但我在 5.2 ,并且不确定如何在这里得到我想要的...建议(除了在 ubuntu 上编译 5.3 之外)?
I'm looking to do something like get the opposite match from this line:
$input = "21.asdf*234true;asdf0--11"
$_BOOL_ ="/(true|false)/i";
$output = preg_replace($_BOOL_, '', $input);
//Result: "21.asdf*234;asdf0--11"
//Desired result: "true"
Which ofcourse in php 5.3 is
$output = preg_filter($_BOOL_, '', $input);
But I'm on 5.2, and not sure how to get what I want here... Suggestions (other than compile 5.3 on ubuntu)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的例子还是有点模糊。
如果字符串包含 true 或 false,则输出应该为“true”?
如果是这种情况,我认为您正在寻找
preg_match()
。如果您希望在字符串包含 true 时返回“true”,如果字符串包含 false 则返回“false”,我认为您可能必须使用一系列函数(例如 preg_match() 或 strpos())来分别匹配每个条件。
Your example is still a little vague.
If the string contains EITHER true or false, the output should be "true"?
If that is the case you are looking for
preg_match()
I think.If you are looking to return either "true" if the string contains true and "false" if it contains false, I think you may have to use a series of functions such as preg_match() or strpos()to match each condition seperately.
怎么样:
编辑:再次看看你的问题,我不确定我们在谈论同一件事。对于输入数组中的每个字符串,
preg_replace
执行它可以进行的任何替换并返回结果;任何与正则表达式不匹配的字符串都会原封不动地传递。preg_filter
是相同的,只是它会丢弃与正则表达式不匹配的字符串。我不会称这些为对立的。如果这不是您想要的,也许您可以提供一些示例。How about:
EDIT: Looking at your question again, I'm not sure we're talking about the same thing. For each string in the input array,
preg_replace
performs any replacements it can and returns the result; any string that doesn't match the regex is passed along unchanged.preg_filter
is the same, except it discards the strings that don't match the regex. I wouldn't call those opposites. If this isn't what you're looking for, maybe you can provide some examples.