2017-02-01 15 views
-1

HTMLページを取得しました。どのようにして相対的なimg srcをこれらのURLに置き換えることができますか?私のコンテンツのHTMLで相対srcをフルURLのimgに置き換えます

<img width="23" height="23" class="img" src="/static/img/facebook.png" alt="Facebook"> 

<img width="23" height="23" class="img" src="/static/img/tw.png" alt="tw"> 

(パス安定していない)

<img width="130" =="" height="200" alt="" src="http://otherdomain/files/ads/00905819.gif"> 

HTMLコンテンツでは、私が唯一相対SRC画像を変更したい、他の画像は絶対SRC用いたSRC持っています。 例:

<img width="23" height="23" class="img" src="/static/img/facebook.png" alt="Facebook"> 
    <img width="23" height="23" class="img" src="/images/xx.png" alt="xxx"> 

<img width="23" height="23" class="img" src="http://example.com/static/img/facebook.png" alt="Facebook"> 

への私にpreg_replace:

$html = preg_replace("(]*src\s*=\s*[\"'])(?!http)([^\"'>]+)([\"'>]+)", '$1http://www.example.com$2$3', $x1);

しかし、あなたはあなたの相対パスは、常に、その後'/static/img/'で始まることがわかっている場合、これは

答えて

1

SRCすべての画像を置き換えます代わりに単純なstr_replace関数を使用することができますf regexp。例えば

$html = str_replace('src="/static/img/', 'src="http://example.com/static/img/', $html); 
+0

ありがとうございます。 – lock

1

は、このコードを使用してみてください、 は、それはあなたのケースで動作するはずです。

$html = str_replace('src="/static', 'src="http://example.com/static', $html); 
関連する問題