2012-02-07 21 views
2

正規表現の問題正規表現が異なる動作

正規表現コードでグラフィックファイルを処理しています。 Webページで

$view[content] = preg_replace("/(\<img)([^\>]*)(\>)/i", "\\1 name='target_resize_image[]' onclick='image_window(this)' style='cursor:pointer;' \\2 \\3", $view[content]); 

、存在しない場合は「スタイル 『htmlコードには、このコードは正常に動作します。しかしがある場合。『スタイル』は、『スタイルスタイル』のコードをに変更しました』 =」カーソル:ポインタ; "。

imgに" style = '...' "がある場合は、style = '...'"が追加されます。そうでなければ、 "スタイル"コードは "style = 'cursor:pointer;'"でなければなりません。

"preg_replace"はimgコードで "style = 'aaaaaa'を取り除きます。これは "style = 'cursor:pointer'"でなければなりません。

コード入力

<img style="border-bottom: medium none; border-left: medium none; width: 160px; float: left; height: 160px; border-top: medium none; margin-right: 1em; border-right: medium none" alt="120131_12e1c8be2d6954ec9a3579ae57a64bfe_3EsTQWri2Zx9.gif" src="/data/cheditor4/1201/120131_02133169e006e1d08fc72fa5ff1e7a25_5KKOd8zrZhluXoqpiN.gif" /> 

コード出力

<img style="cursor:pointer" alt="120131_12e1c8be2d6954ec9a3579ae57a64bfe_3EsTQWri2Zx9.gif" src="/data/cheditor4/1201/120131_02133169e006e1d08fc72fa5ff1e7a25_5KKOd8zrZhluXoqpiN.gif" /> 

コードは、 - 任意の役に立つコメントが理解されるであろう

<img style="cursor:pointer;border-bottom: medium none; border-left: medium none; width: 160px; float: left; height: 160px; border-top: medium none; margin-right: 1em; border-right: medium none" alt="120131_12e1c8be2d6954ec9a3579ae57a64bfe_3EsTQWri2Zx9.gif" src="/data/cheditor4/1201/120131_02133169e006e1d08fc72fa5ff1e7a25_5KKOd8zrZhluXoqpiN.gif" /> 

であるべきです。

+6

outoutでしょうか? 'img {カーソル:ポインタ; } '...問題が解決しました。 – bummzack

答えて

4

あなたの正規表現は、この出力を作成します。

<img name='target_resize_image[]' onclick='image_window(this)' style='cursor:pointer;' style="border-bottom: medium none; border-left: medium none; width: 160px; float: left; height: 160px; border-top: medium none; margin-right: 1em; border-right: medium none" alt="120131_12e1c8be2d6954ec9a3579ae57a64bfe_3EsTQWri2Zx9.gif" src="/data/cheditor4/1201/120131_02133169e006e1d08fc72fa5ff1e7a25_5KKOd8zrZhluXoqpiN.gif"/> 

course- 2スタイルがこの文字列の属性-ofあなたが持っていることに、注意してください。どうやら、何らかのHTML検証を使用しています.2番目の検証を自動的に「修正」し、style='cursor:pointer;'というままにしています。

はあなたの正規表現を改善します。 /(alt|style|src)=("[^"]*")/ipreg_match_allのパターンを使用すると、imgタグの属性を抽出し、それを操作して新しいHTML文字列を作成できます。

とにかく、RegExpのを使ってHTMLを操作することはまったくお勧めできません。 DOMツールを使う方がはるかにシンプルでロバストです。 Simple HTML DOM Parserをご覧ください。この単純なコード

require 'simple_html_dom.php'; // http://simplehtmldom.sourceforge.net/ 
$html = str_get_html($img); // load HTML as DOM object 
$img = $html->find('img', 0); // find your tag 
$img->style = "cursor:pointer;".$img->style; // manipulate the style attr 
$img->name="target_resize_image[]"; // set other attr's 
$img->onclick="image_window(this)"; 
echo htmlspecialchars($html); // output HTML as string 

はあなたに出力

<img style="cursor:pointer;border-bottom: medium none; border-left: medium none; width: 160px; float: left; height: 160px; border-top: medium none; margin-right: 1em; border-right: medium none" alt="120131_12e1c8be2d6954ec9a3579ae57a64bfe_3EsTQWri2Zx9.gif" src="/data/cheditor4/1201/120131_02133169e006e1d08fc72fa5ff1e7a25_5KKOd8zrZhluXoqpiN.gif" name="target_resize_image[]" onclick="image_window(this)" /> 

出来上がりを与えます!また、属性の順序、使用される引用符、スペース、改行には依存しません。

EDIT:WindStoryは非DOMソリューション

を要求したソリューションは、あなたの入力文字列の与えられたstrcutureに非常に多くを依存しています。

$img = str_replace('style="', 'style="cursor:pointer; ', $img); // add info to the given style-attr 
$img = str_replace('<img', '<img new_attribute="value" ', $img); // add new attrs 

があなたの特定のタグに少ない保証を持って、あなたは正規表現Iを使用することができます:あなたは、たとえば、すべてのimg与えられたが、すでにstyle属性を持っていることを、確実にわかっている場合は、交換する平野でそれを行うことができますimgタグの属性

// extract attr's, depends on double quotes 
$Attrs = array(); 
if (preg_match_all('/(alt|style|src)="([^"]*)"/i', $img, $matches, PREG_SET_ORDER)) { 
    foreach ($matches as $match) { 
     $Attrs[$match[1]] = $match[2]; 
    } 
} 

// change/add attr's 
$Attrs['style'] = empty($Attrs['style']) ? 'cursor:pointer' : 'cursor:pointer; '.$Attrs['style']; 
$Attrs['name'] = 'target_resize_image[]'; 
$Attrs['onclick'] = 'image_window(this)'; 

// build new HTML 
$new_img = '<img '; 
foreach ($Attrs as $key => $value) $new_img .= $key.'="'.$value.'" '; 
$new_img .= '/>'; 

を抽出するために、上記のアクションhereでそれを参照してください。

希望に役立ちます。

EDIT 2:WindStoryはここでより多くのフォールトトレラントのRegExpソリューションを

を求めているが追加する/変更が指定したHTML文字列の属性、小さな関数です。一重引用符が二重引用符で囲まれている場合は、引用符は一重引用符または二重引用符でサポートされますが、引用符が見つからない場合は機能しません。

function add_attr($html, $name, $value, $append = null) { 
    $attr_pattern = "/\b({$name}=['\"])([^'\"]*)(['\"])/i"; 
    if (preg_match($attr_pattern, $html, $regs)) { 
     if (!is_null($append)) { 
      $value = $regs[2].$append.$value; 
     } 
     $replace = "\\1$value\\3"; 
     $html = preg_replace($attr_pattern, $replace, $html); 
    } else { 
     $tag_pattern = '/<[\w]+\b/i'; 
     $replace = "\\0 $name=\"$value\""; 
     $html = preg_replace($tag_pattern, $replace, $html); 
    } 
    return $html; 
} 

$htmlとしてのhtmlタグを挿入属性の$nameを定義し、$valueを定義します。属性がすでに存在する場合は値が置換され、そうでない場合は追加されます。 $appendを設定すると、$valueが追加され、$appendが連結記号として使用されます。

この

$img = add_attr($img, 'style', 'cursor:pointer', '; '); 
$img = add_attr($img, 'name', 'target_resize_image[]'); 
$img = add_attr($img, 'onclick', 'image_window(this)'); 
echo htmlspecialchars($img); 

あなたは右、スタイルシートの認識している

<img onclick="image_window(this)" name="target_resize_image[]" style="border-bottom: medium none; border-left: medium none; width: 160px; float: left; height: 160px; border-top: medium none; margin-right: 1em; border-right: medium none; cursor:pointer;" alt="120131_12e1c8be2d6954ec9a3579ae57a64bfe_3EsTQWri2Zx9.gif" src="/data/cheditor4/1201/120131_02133169e006e1d08fc72fa5ff1e7a25_5KKOd8zrZhluXoqpiN.gif" /> 
+0

DerVO /あなたの種類と詳細な返信をいただきありがとうございます。私はdomパーサー以外の方法を持っていたいと思います。そのため、このコードはレコードを照会するために機能します。可能であれば、もっと簡単な正規表現を教えてください。 – WindStory

+0

貴重なご返信ありがとうございます。しかし、私は追加の質問をしたいと思います。 私はstyle = "とstyle = 'を盛り上げてみたいので、このコードのヒントをもう一つお知らせください。 img); //指定されたスタイルに情報を追加する-attr' あなたの指示に従って、 "style =" "はうまく動作しますが、" style = '"があります。 さらにお返事をお待ちしています。 – WindStory

+0

私の2番目の編集を見てください – DerVO

関連する問題