preg_replace 没有按照我需要的方式工作
以下内容无法按照我需要的方式工作:
$homepage = file_get_contents('www.site.com');
$var1= preg_replace('#ProdSupp\*(.*?)\*ProdSupp#siu', '$1', $homepage);
当它归档内容时,它确实会在 ProdSupp* *ProdSupp
中找到内容,但是 var1 在获取其查找的字符串后存储了整个内容for....我怎样才能消除 var1 中的所有内容,只让它存储 ProdSupp* *ProdSupp
之间的内容?
我不知道如何消除垃圾文本,只存储 ProdSupp* *ProdSupp
之间的值。有什么想法吗?
The following is not working the way I need it to work:
$homepage = file_get_contents('www.site.com');
$var1= preg_replace('#ProdSupp\*(.*?)\*ProdSupp#siu', '$1', $homepage);
when it file contents the site it DOES find the stuff inside ProdSupp* *ProdSupp
, but var1 stores the ENTIRE content after it fetches the string its looking for....how can I eliminate EVERYTHING in var1 and ONLY make it store the stuff between ProdSupp* *ProdSupp
?
I dont know how to eliminate the junk text and only store the value between ProdSupp* *ProdSupp
. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“My Regex Tester”(这不是我的,只是它的名字)是一个方便的工具,用于调试诸如这。如果我正确地阅读了您尝试执行的操作,则似乎您实际上应该使用
preg_match_all()
而不是preg_replace()
。因此,总而言之,您要查找的内容位于
$matches[1][0]
中。"My Regex Tester" (it's not mine, it's just what it's called) is a handy tool for debugging stuff like this. If I read what you're trying to do correctly, it seems like you should actually be using
preg_match_all()
instead ofpreg_replace()
.So when all is said and done, the contents you're looking for are in
$matches[1][0]
.正如已经提到的,如果我正确理解你的问题,你可能应该使用某种 preg_match 函数。
如果您想同时进行替换和获取匹配项,您可以将 preg_replace_callback 与全局变量一起使用,如下所示:
在 $var1 数组中将是括号内的匹配项,在 $var11 中是整个修改的内容。
As was already mentioned, you should probably used some kind of preg_match function, if I understood your question correctly.
If you would like to both make replacements and get matches, you could use preg_replace_callback with global variable like this:
In $var1 array will be your matches inside brackets and in $var11 the whole modified content.