doukanzhuo4297 2016-04-26 02:57
浏览 24
已采纳

在php和wamp中使用<>

This must be terribly well known, but I can't find anything relevant. Strings in php cant have the characters < and > in them in the wamp environment. It all works fine in the live server. e g under wamp

$teststring = 'aaa<bbb';
echo $teststring;

produces aaa.

I want to edit html files using str_replace() and preg_replace(). I guess I have to modify the php set up but I don't know how.

  • 写回答

1条回答 默认 最新

  • duan19913 2016-04-26 03:11
    关注

    I assume it doesn't have anything to do with WAMP, and I'm not even sure I understand the situation fully, because you say "Strings in php cant have the characters <> in them in the wamp environment" (I interpret it as: It doesn't work with WAMP) and then directly afterwards "It all works fine in the live server. e g under wamp" (I interpret it as: It does work with WAMP).

    But I believe the problem is just that you are adding stray unencoded <'s into the HTML output. Think about what would happen normally:

    <?php echo "I can write <em>emphasized</em> text!"; ?>
    

    ...would result in:

    I can write emphasized text!

    ...and not:

    I can write <em>emphasized</em> text!

    Because you can output HTML from PHP, and the browser will read it as it would read any static HTML page. Now if you just include a random <, it will be interpreted as HTML as said in the comments and will not be valid.

    So, in order to have a literal < shown in the browser, it has to be encoded as HTML entity, in this case &lt;, e.g. 3 &lt; 4 instead of 3 < 4. This can be automatically done using the function htmlentities. For example:

    <?php echo htmlentities("This is a string with < and > and & and other stuff like this which has to be encoded."); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部