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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?