[英]preg_match pattern to find the contents of a string between <html> and </html> tags
[英]Preg match contents between [IMG] tags
我正在寫一個腳本來檢索帖子中的第一個圖像鏈接
$content = [center]Hello World, this is my house: [img]myhouse.png[/img] blah blah blah blah [/center] This is another house [img]anotherhouse.png[/img] , blah blah blah
我只想返回“ myhouse.png”並將其保存到變量中img
應該不區分大小寫,這意味着它可以在[img]text-here[/img]
或[IMG]text-here[/IMG]
這將返回第一個圖像:
$content = '[center]Hello World, this is my house: [img]myhouse.png[/img] blah blah blah blah [/center] This is another house [img]anotherhouse.png[/img] , blah blah blah';
preg_match('#\[img\]\s*(?P<png>.*?)\s*\[/img\]#i', $content, $m);
echo $m['png']; // myhouse.png
這是一個正則表達式:
$content = '[center]Hello World, this is my house: [img]myhouse.png[/img] blah blah blah blah [/center] This is another house [img]anotherhouse.png[/img] , blah blah blah';
$pattern = '/\[img\](.*)\[\/img\]/U'; // <-- the U means ungreedy
preg_match_all($pattern, $content, $matches);
var_dump($matches[1]);
說明:
正則表達式匹配一對[img] ... [/img]
標簽之間的所有內容。 為了確保它不匹配第一個[img]
和最后一個[/ img]標簽之間的所有文本,我使用了ungreedy修飾符。
了解有關PHP的正則表達式語法的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.