[英]Preg_replace to get Image link in Content PHP
我正在用PHP5和MySql編寫代碼。 它包括標題,內容。 在內容中,包含圖片鏈接。 我想在Content中獲取圖像鏈接,所以我用Preg_replace做到了。 但是效果不好,有時Image鏈接包含另一個標簽Preg_replace無法與此Image鏈接一起使用。
這是我的代碼
preg_match('/<img.+src=[\'"](?P<src>.+)[\'"].*>/i', $Content, $image);
$image = preg_replace( '/(width|height)="\d*"\s/', "", $image );
$image = preg_replace( '/"/', "", $image );
$image = preg_replace( '/height=\d*/', "", $image );
$image = preg_replace( '/(width|height)=\d*/', "", $image );
我盡力減少圖像鏈接中的標簽。 但是有時,我從另一個網站復制的內容卻不包含“圖片”鏈接為:
<img alt="" height="20" src="http://anotherWebsite.com/images/2014/share-gg.gif" style="border: 0px; max-width: 400px;" width="67" />
而且它不是固定圖像鏈接。
因此,請幫助我使用Preg_replace可以很好地處理所有圖像鏈接。 謝謝!
P / s:內容由CkEditor創建
您是否正在尋找類似的東西?
preg_match('/<img.+src=[\'"](?P<src>.+)[\'"].*>/i', $Content, $image);
preg_match('/src="(.*?)"/',$image[0],$image);
$image = (isset($image[1]))?$image[1]:'';
它使用src搜索img標簽,然后在找到的標簽內搜索src的內部內容
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.