在 php 和 wamp 中使用 <>

use of <> in php and in wamp

这一定是众所周知的,但我找不到任何相关的东西。 php 中的字符串在 wamp 环境中不能包含字符 <>。在实时服务器中一切正常。例如 wamp

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

产生 aaa

我想使用 str_replace()preg_replace() 编辑 html 个文件。 我想我必须修改 php 设置,但我不知道该怎么做。

我假设它与 WAMP 没有任何关系,我什至不确定我是否完全了解情况,因为你说 "Strings in php cant have the characters <> in them in the wamp environment"(我将其解释为:它不起作用使用 WAMP)然后直接 "It all works fine in the live server. e g under wamp"(我将其解释为:它确实适用于 WAMP)。

但我认为问题在于您将杂散的未编码 < 添加到 HTML 输出中。想想正常情况下会发生什么:

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

...将导致:

I can write emphasized text!

...而不是:

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

因为您可以从 PHP 输出 HTML,浏览器将像读取任何静态 HTML 页面一样读取它。现在,如果您只包含一个随机的 <,它将被解释为 HTML 和 ,并且将无效。

因此,为了在浏览器中显示文字 <,必须将其编码为 HTML 实体,在本例中为 &lt;,例如3 &lt; 4 而不是 3 < 4。这可以使用函数 htmlentities 自动完成。例如:

<?php echo htmlentities("This is a string with < and > and & and other stuff like this which has to be encoded."); ?>