qinHaiXiang
qinHaiXiang

Reputation: 6419

When adding a "<" in front of a string it isn't displayed anymore

Whan happen when I add 'smaller than < ' before the string ,because the string which has it in front was gone away [ didn't display when I echo them ] in my PHP code。

E.g:
$search = trim('wdisappear W');  //  all the words were disappeared when < in front
$search = explode(" ", $search); //
$sizeof_search = count($search);  //


for($i = 0; $i < $sizeof_search; $i++){
    $l = strlen($search[$i]);
    echo '<'.$search[$i].'<'.$l;
}

When I open the php file in firefox . 'wdisappear W' doesn't appear! Why??

And how can I put the

<

in front??

Thank you very much!!

Upvotes: 1

Views: 151

Answers (3)

Mark Byers
Mark Byers

Reputation: 838336

The character < has special meaning in HTML as the start of a tag. Call htmlspecialchars to convert the < to the HTML entity &lt; so that it displays correctly in the browser. You will probably also want to call this on the value of $search[i], otherwise you could leave a cross-site scripting vulnerability in your application.

Try this:

echo htmlspecialchars('<' . $search[$i] . '<' . $l);

From the documentation:

Certain characters have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings. This function returns a string with some of these conversions made; the translations made are those most useful for everyday web programming.

Upvotes: 3

NikiC
NikiC

Reputation: 101936

By default PHP delivers content as text/html and in HTML < opens a tag. Thus your content is interpreted as an invalid tag and is not displayed. You should escape it using &lt;. There are other reserved characters, that need to be encoded. You can do all at once using htmlspecialchars.

Alternatively you could deliver your content as text/plain, which does not require any escaping. (On the downside, you can't use HTML in there, obviously.)

Upvotes: 4

GolezTrol
GolezTrol

Reputation: 116110

That's because < opens a html element. This will cause a part of the text to be invisible, because FF thinks it is meant as HTML markup. Escape it by outputting the &lt; html entity, or use htmlspecialchars function to escape the output for you.

Upvotes: 7

Related Questions