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

报告相同问题?

悬赏问题

  • ¥30 使用matlab将观测点聚合成多条目标轨迹
  • ¥15 Workbench中材料库无法更新,如何解决?
  • ¥20 如何推断此服务器配置
  • ¥15 关于github的项目怎么在pycharm上面运行
  • ¥15 内存地址视频流转RTMP
  • ¥100 有偿,谁有移远的EC200S固件和最新的Qflsh工具。
  • ¥15 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析