2016-10-26 15 views
-2

ページコンテンツを読み込んで自分のウェブサーバーに投稿するクロムエクステンションを作成しました。問題は、src属性をhtmlソースからWebサイトのアドレスに変更する必要があることです。 ....イメージのsrc属性をHTMLコンテンツから置き換えます。

$html = '<p style="font-size: 12px; font-family: Tahoma; text-align: center;"><img style="font-size: 12px; font-family: Tahoma;" src="http://sga.ufmg.br/images/uploads/13212/MSI_civil_01.png" alt="" width="338" height="367"></p>'; 

$img = 'http://sga.ufmg.br/images/uploads/13212/MSI_civil_01.png'; 

$result = str_replace($img, 'http://asrespostazzz.com.br/questoes_img/' . $new_img_name, $html); 

それだけで文句を言わない仕事:(

私はpreg_replaceを使用しようとしましたが、私にエラーを与えた:

(これは単なる一例である)私のコードに行くことができます:あなたは、HTML内の単一の画像要素を持っている場合は

Message: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash

+0

このエラーは、コードの投稿や使用方法をサポートしていません。マニュアルをもう一度読んでください。 –

+0

それは私のために働く、srcは常に同じものですか? –

答えて

0

このpreg_replaceは動作します:

preg_replace('/<img\ [^>]+src="([^"]+)"/', 'http://asrespostazzz.com.br/questoes_img/' . $new_img_name, $html); 

しかし、複数の画像要素がある場合は、置き換えて配列を作成する必要があります(私はそれがあなたの場合に近づきやすいかどうかわかりません...もっと詳しい情報が参考になります)。したがって、preg_replaceコードは次のようになります。

$replacements = [ 
     'http://asrespostazzz.com.br/questoes_img/' . $new_img_name, 
     ..... 
]; 
preg_replace('/<img\ [^>]+src="([^"]+)"/', $replacements, $html);