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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵