Php ampersand in line

I have a problem. I am trying to create an IRC bot that has an ampersand in its password. However, it is difficult for me to put an ampersand in a string. For instance...

<?php

$var = "g&abc123";

echo $var;

?>

I believe this should print g&abc123. However, the seal g.

I also tried this:

<?php
$arr = array("key" => "g&abc123");
print_r($arr);
?>

This is correctly printed using g&abc123, however, when I say echo $arr['key'];it prints again g. Any help would be greatly appreciated. I am running PHP5.3.1.

EDIT . I also noticed that if I use g&abc123&abc123, it prints g&abc123. Any suggestions?

+5
source share
4 answers

I do not have this problem in the console:

php > $d="g&abc123";
php > echo $d;
g&abc123

? , - HTML. , &amp;.

+5

, .

, HTML, htmlentities &amp;

+5

-, , .

+1

, -.

HTML, XHTML XML . . escape-.

, PHP :

$variable = 'It Friday';

, PHP , .

:

$variable = 'It\ Friday';

Similarly, in HTML and XHTML you cannot say

<h1>Inequalities</h1>
<p> x<yz+3 </p>

This is because it will be interpreted as an element.

Instead, you will need to say:

<h1>Inequalities</h1>
<p> x&lt;yz+3 </p>

Now, as you can see, the ampersand itself has special meaning and therefore must be escaped as &. htmlspecialchars () will do this for you.

+1
source

All Articles