![](/img/trans.png)
[英]PHP: change [img]src[/img] to <img src=“src” alt=“src” > with regular expression
[英]Regular expression to find “src” attribute of HTML “img” element in PHP
我有一個字符串,里面有一個圖像:
"<p><img src="http://yahoo.com/testfolder/userdata/editoruploadimages/confused man.jpg" /></p>"
我無法使用正則表達式獲取圖像URL。 我的代碼是:
preg_match_all("/src=([^\\s]+)/", $questArr_str, $images);
此代碼在遇到映像名稱中的空格時停止執行。 它只返回"http://yahoo.com/testfolder/userdata/editoruploadimages/confused
返回的字符串應為: "http://yahoo.com/testfolder/userdata/editoruploadimages/confused man.jpg"
我會抓住引號內的所有內容:
preg_match_all('/src="([^"]+)"/', $questArr_str, $images);
讀取的部分([^\\s]+)
表示選擇任何不是空格的東西。
也許嘗試類似的東西:
/src="([^"]+)"/
哪個是選擇任何不是雙引號的東西。
感謝每一個人幫助我。
我找到了我的解決方案:
pattern = "/src=([^\\\"]+)/"
這是一種簡單的方法來匹配<img />
標簽src
屬性和/或html/PHP
內容與正則表達式。
樣品:
<img class="img img-responsive" title="publisher.PNG" src="media/projectx/agent/author/post/2/image/publisher.PNG" alt="" width="80%" />
僅匹配src
屬性內容使用
preg_match("%(?<=src=\")([^\"])+(png|jpg|gif)%i",$input,$result)
$result[0]
將輸出media/projectx/agent/author/post/2/image/publisher.PNG
匹配`src'屬性和它的內容使用
preg_match("%src=\"([^\"])+(png|jpg|gif)\"%i",$input,$result)
$result[0]
將輸出src="media/projectx/agent/author/post/2/image/publisher.PNG"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.