![](/img/trans.png)
[英]How do I get the text inside of the <a> This Text </a> tags using cURL?
[英]How do I get text containing a certain string between tags
请帮我解决preg_match,我想不通:(
我有很多文本,但我需要捕获包含特定文本的“&”之间的所有内容。
例子
"thisip4:isatextexample&ineed.thistext&TXT:&andthis.idontneed&txt:&test.thistext&"
我需要提取包含此文本的 & 之间的完整文本
结果应该是: ineed.thistext
AND : test.thistext
非常感谢提前:)
哦,我试过用这个;
&([^\\n]*thistext[^\\n]*)&
但这不适用于多个“&”
宽
您的模式包含[^\\n]*
匹配除换行符以外的任何 0+ 个字符,这使得正则表达式引擎贪婪地匹配任何&
字符并找到该行的最后一个&
。
您可以使用
'~&([^&]*?thistext[^&]*)&~'
然后,获取 Group 1 值。 请参阅正则表达式演示。
细节
&
- 一个&
字符([^&]*?thistext[^&]*)
- 捕获组 1:
[^&]*?
- 除&
之外的任何 0+ 个字符,尽可能少thistext
- 文字文本[^&]*
- 除&
之外的任何 0+ 个字符,尽可能多&
- 一个&
字符$str = 'thisip4:isatextexample&ineed.thistext&TXT:&andthis.idontneed&txt:&test.thistext&';
if (preg_match_all('~&([^&]*?thistext[^&]*)&~', $str, $m)) {
print_r($m[1]);
}
// => Array ( [0] => ineed.thistext [1] => test.thistext )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.