开发者

PHP string cut short

开发者 https://www.devze.com 2023-01-20 22:23 出处:网络
Why does this code $st开发者_如何学Cring = \"!@#$%^&*(<a@g.com\"; echo $string; only output:

Why does this code

$st开发者_如何学Cring = "!@#$%^&*(<a@g.com"; 
echo $string; 

only output:

!@#$%^&*(

Is this is a PHP bug?


Because < is a reserved character in in HTML :)

Use &lt; and &gt;

Read this for more information

http://www.w3schools.com/HTML/html_entities.asp

You can use the function htmlspecialchars to convert such special chars

http://php.net/manual/en/function.htmlspecialchars.php


I'm not seeing that:

http://ideone.com/zhycx

Perhaps you've got some weird characters in your file? Make sure you're using a "normal" encoding on your source code, as well.


You need to do:

echo htmlentities($string);

to display the string as it is on a browser. This is because the < in the string is interpreted by the browser as start of a HTML tag.

So it's not PHP but the browser that is causing this behavior. If you do the exact same display on a command line, you'll see all the characters.


If you are viewing the output in a web browser, then the < begins a tag and is usually not displayed but interpreted in the HTML document structure parser. Also, a $ inside of a double-quoted string is interpolated as the variable name that follows it; try using single quotes where this won't happen.

Try this:

$string = '!@#$%^&*(<a@g.com';
echo htmlentities($string);
0

精彩评论

暂无评论...
验证码 换一张
取 消