[英]How do I get text containing a certain string between tags
Please help me out with preg_match, I can't figure it out :(请帮我解决preg_match,我想不通:(
I have a lot of text but I need to capture everything between "&" that contains a certain text.我有很多文本,但我需要捕获包含特定文本的“&”之间的所有内容。
example例子
"thisip4:isatextexample&ineed.thistext&TXT:&andthis.idontneed&txt:&test.thistext&"
I need to extract the complete text between & containing thistext我需要提取包含此文本的 & 之间的完整文本
the result should be : ineed.thistext
AND : test.thistext
结果应该是:
ineed.thistext
AND : test.thistext
Many many many thanks in advance :)非常感谢提前:)
oh I've tried using this;哦,我试过用这个;
&([^\\n]*thistext[^\\n]*)&
but that will not work with multiple '&'但这不适用于多个“&”
W宽
Your pattern contains [^\\n]*
that matches any 0+ chars other than newlines, and that makes the regex engine match across any &
chars greedily and find the last &
on the line.您的模式包含
[^\\n]*
匹配除换行符以外的任何 0+ 个字符,这使得正则表达式引擎贪婪地匹配任何&
字符并找到该行的最后一个&
。
You may use您可以使用
'~&([^&]*?thistext[^&]*)&~'
Then, grab Group 1 value.然后,获取 Group 1 值。 See the regex demo .
请参阅正则表达式演示。
Details细节
&
- a &
char &
- 一个&
字符([^&]*?thistext[^&]*)
- Capturing group 1: ([^&]*?thistext[^&]*)
- 捕获组 1:
[^&]*?
- any 0+ chars other than &
, as few as possible &
之外的任何 0+ 个字符,尽可能少thistext
- literal text thistext
- 文字文本[^&]*
- any 0+ chars other than &
, as many as possible [^&]*
- 除&
之外的任何 0+ 个字符,尽可能多&
- a &
char &
- 一个&
字符$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.