[英]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.