PHPで文字列置換をやっています。
入力ページから送られてきた「文、変更する文字列、変更後の文字列」を表示する画面で置換をして、表示します。
input.PHP
1<!DOCTYPE html> 2<html lang="ja"> 3 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 6 <title>入力</title> 7 </head> 8 9 <body> 10 <h2>入力画面</h2> 11 12 <form method="post" action="chara_result.php"> 13 <table> 14 <tr> 15 <td valign="top">文</td> 16 <td><textarea name="change"></textarea></td> 17 </tr> 18 <tr> 19 <td>変更文字列</td> 20 <td><input type="text" name="change_before" value="" /></td> 21 </tr> 22 <tr> 23 <td>変更後文字列</td> 24 <td><input type="text" name="change_after" value="" /></td> 25 </tr> 26 </table> 27 <input type="submit" name="check" value="変更" /> 28 </form> 29 </body> 30</html> 31
置換して表示される文の変更された箇所だけ色をつけて表示したいのですがXSS対策もいれてますのでうまく表示されません
output.php
1<?php 2 if (isset($_POST["change"])) { 3 $write = $_POST["change"]; 4 $write_before = $_POST["change_before"]; 5 $write_after = $_POST["change_after"]; 6 7 //CRLFを改行1文字にする 8 $write = str_replace("\r\n", "\n", $write); 9 10 //未入力チェック 11 if (($write == "")||($write_before == "")||($write_after == "")) { 12 $result = "入力してください。"; 13 //文入力数チェック 14 } elseif (mb_strlen($write, 'utf-8') > 100) { 15 $result = "100文字以内で入力してください。"; 16 //変更後入力数チェック 17 } elseif (mb_strlen($write_before, 'utf-8') > 100) { 18 $result = "100文字以内で入力してください。"; 19 //変更後入力数チェック 20 } elseif (mb_strlen($write_after, 'utf-8') > 100) { 21 $result = "100文字以内で入力してください。"; 22 } else { 23 $after = "<span style='color: #ff0000'>$write_after</span>"; 24 //文字列置換 25 $result = str_replace($write_before, $write_after, $write); 26 } 27 } else { 28 $result = "入力していません。"; 29 } 30?> 31<!DOCTYPE html> 32<html> 33 <head> 34 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 35 <title>結果</title> 36 </head> 37 <body> 38 <h2>結果画面</h2> 39 <table> 40 <tr> 41 <td valign="top">結果</td> 42 <td valign="top"> 43 <?php 44 $result = htmlspecialchars($result, ENT_QUOTES, "UTF-8"); 45 echo nl2br($result); 46 ?> 47 </td> 48 </tr> 49 </table> 50 <a href="chara_input.php">入力画面へ</a> 51 </body> 52</html>
htmlspecialcharsでエスケープされる[<>"&]など変更するとき変換語[< > " &]ではなく記号のまま変換でき、、尚且つ表示時に変更された文字の色を変えたい。
上のプログラムだと表示されるのが色変更のコードも表示されます
ヒントでもいいのでお願いします
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/28 05:46
2018/06/28 08:22
2018/06/29 03:01