前に投稿していた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>
result.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 $write_change = htmlspecialchars($write_after, ENT_QUOTES, "UTF-8"); 24 //置換後文字列の色を変える 25 $write_after = "<span style='color: #ff0000'>$write_change</span>"; 26 //文字列置換 27 $result = htmlspecialchars(str_replace($write_before, $write_after, $write), ENT_QUOTES, "UTF-8"); 28 $result = str_replace(htmlspecialchars($write_after, ENT_QUOTES, "UTF-8"), $write_after, $result); 29 } 30 } else { 31 $result = "入力していません。"; 32 } 33?> 34<!DOCTYPE html> 35<html> 36 <head> 37 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 38 <title>結果</title> 39 </head> 40 <body> 41 <h2>結果画面</h2> 42 <table> 43 <tr> 44 <td valign="top">結果</td> 45 <td valign="top"> 46 <?php 47 echo nl2br($result); 48 ?> 49 </td> 50 </tr> 51 </table> 52 <a href="chara_input_test02.php">入力画面へ</a> 53 </body> 54</html>
上の二つのPHPで入力フォームに
<span style='color: #ff0000'>tt</span>
aaaa
変換前文字列に「aa」変換後文字列に「t」といれると<span>~</span>の文字列が赤文字で「t」だけ出力されます。
他の出力は大丈夫でしたがこれだけです。
解決方法ってありますか?
回答2件
あなたの回答
tips
プレビュー