0

Ok, I am using PHP to parse XML file and to display it;s context to HTML. Here is the code. Sample text is - Don't Give UP!

$xml = simplexml_load_file('data/quotes.xml');
            foreach ($xml as $quote) {
                $text = $quote->text;
                echo '<div class="itemWrapper">'.
                            '<div class="quoteHolder">'.
                                    '<p class="quote">'.$text.'</p>'.
                            '</div>'.

                            '<form class="selectionButtons">'.
                                    "<input type='hidden' value='$text' name='quote'>".
                                    '<input class="submitButton" type="button" value="create your design">'.
                            '</form>'.
                     '</div>';
            }

So, when I use $text variable in paragraph it display's correctly, But when I pass it to the form's hidden field I only get: Don (so it stops right before that single quote) It happens with every text that has quotes. Why is that and what is wrong here?

4

2 回答 2

1

HTMLinput字段要求对内容进行转义。方便的是,PHP 有一个函数可以为您完成所有工作:

$display_text = "Don't give up!";
$input_text = htmlspecialchars($text);

参考

于 2013-08-07T02:36:10.440 回答
1

尝试使用这个...

$this_text = "Don't give up!";
$text = htmlspecialchars($this_text, ENT_QUOTES);

echo "<input type='text' value='$text' />";

我已经测试过了。。

于 2013-08-07T02:46:20.187 回答