*検索で、文字参照の&を取り去った文字列を検索した場合の出力の不都合 [#vcaa26f6]

-ページ: BugTrack
-投稿者: [[reimy]]
-優先順位: 普通
-状態: 完了
-カテゴリー: 本体バグ
-投稿日: 2003-05-27 (火) 17:06:00
-バージョン: 

**メッセージ [#n556e2b0]
文字参照の&を取り去った文字列(copy、tradeなど)を検索した場合に、文字参照文字の&が&のまま出力されてしまう。

tradeを検索して、ページ出力した場合の例
 JAVA&<strong class="word0">trade;</strong>

文字参照に含まれる文字列を検索した場合の処理にちょっと工夫が要りそうです。…というか、直前に&がある場合の処理というべきかな。
----
-「タグまたは実体参照または検索語にマッチさせるパターン」と、「タグおよび実体参照はそのまま、検索語は<strong>~</strong>で囲んで返すコールバック関数」をループ内で生成して処理してみました。何というか…無駄の多いやり方ですが、タグ内および実体参照を壊さないようになっています。 -- [[ぱんだ]] &new{2003-05-27 (火) 18:41:29};
 diff -u -r1.77 html.php
 --- html.php	26 May 2003 13:57:04 -0000	1.77
 +++ html.php	27 May 2003 09:42:40 -0000
 @@ -83,9 +83,10 @@
  		{
  			$s_key = htmlspecialchars($key);
  			$search_word .= " <strong class=\"word$id\">$s_key</strong>";
 -			$to = "<strong class=\"word$id\">\$1</strong>";
 -			$body = preg_replace("/(?:^|(?<=>))([^<]*)/e",
 -				'preg_replace("/($pattern)/",$to,\'$1\')',$body);
 +			$body = preg_replace_callback(
 +				"/(<[^>]*>|&(?:#[0-9]+|#x[0-9a-f]+|[0-9a-zA-Z]+);)|($pattern)/",
 +				create_function('$arr',
 +					'return $arr[1] ? $arr[1] : "<strong class=\"word'.$id.'\">{$arr[2]}</strong>";'),$body);
  			$id++;
  		}
  		$body = "<div class=\"small\">$_msg_word$search_word</div>$hr\n$body";
-あ…だめだ。実体参照そのものが置換できなくなってる。 -- [[ぱんだ]] &new{2003-05-27 (火) 19:22:20};
-これでどうでしょうか。 -- [[ぱんだ]] &new{2003-05-27 (火) 20:00:37};
 diff -u -r1.77 html.php
 --- html.php	26 May 2003 13:57:04 -0000	1.77
 +++ html.php	27 May 2003 10:59:09 -0000
 @@ -83,9 +83,12 @@
  		{
  			$s_key = htmlspecialchars($key);
  			$search_word .= " <strong class=\"word$id\">$s_key</strong>";
 -			$to = "<strong class=\"word$id\">\$1</strong>";
 -			$body = preg_replace("/(?:^|(?<=>))([^<]*)/e",
 -				'preg_replace("/($pattern)/",$to,\'$1\')',$body);
 +			$pattern = ($s_key{0} == '&') ?
 +				"/(<[^>]*>)|($pattern)/" :
 +				"/(<[^>]*>|&(?:#[0-9]+|#x[0-9a-f]+|[0-9a-zA-Z]+);)|($pattern)/";
 +			$body = preg_replace_callback($pattern,
 +				create_function('$arr',
 +					'return $arr[1] ? $arr[1] : "<strong class=\"word'.$id.'\">{$arr[2]}</strong>";'),$body);
  			$id++;
  		}
  		$body = "<div class=\"small\">$_msg_word$search_word</div>$hr\n$body";
-うまくいきましたm(__)m -- [[reimy]] &new{2003-05-27 (火) 22:07:57};

#comment
//#comment

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Site admin: PukiWiki Development Team

PukiWiki 1.5.4+ © 2001-2022 PukiWiki Development Team. Powered by PHP 8.2.12. HTML convert time: 0.069 sec.

SourceForge