doukanzhuo4297 2016-04-26 10: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 11: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."); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器