PHP string cut short - php

Why does this code
$string = "!##$%^&*(<a#g.com";
echo $string;
only output:
!##$%^&*(
Is this is a PHP bug?

Because < is a reserved character in in HTML :)
Use < and >
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);

Related

How to Include "<" in the string in PHP

My PHP string has < in its value which is omitting the characters after it, How should I do that
<?php
$abc = escapeshellarg("Amp^[dfdf&c4hcSdf/Z<dfdrV");
echo $abc; // output is Amp^[dfdf&c4hcSdf/Z
// Desired Output is Amp^[dfdf&c4hcSdf/Z<dfdrV
?>
Please help me to understand this
Thank You
The < is included, but your browser thinks it's part of an HTML tag, so hides it. Look at "View Source".
You need to escape it, so that it becomes < which will look like < when displayed in the browser.
You possibly realised this, but grabbed at the wrong function - escaping isn't something you can do "once and for all", it has to be specific to the context where you're using something. escapeshellarg is for escaping strings used in command-line ("shell") commands. The function for escaping for use in HTML is called htmlspecialchars.

quotes not working in php

I am using php. This code does not work:
<?php
$str="<?palash";
print($str);
no output
but it works as soon as I introduce a space between < and ?
<?php
$str="< ?palash";
print($str);// prints '< ?palash'
you can also escape special characters by using a backslash \ before the special character ? that you want to escape
this will do what you're asking for
<?php
$str='<\?palash';
echo $str;
?>
cheers!
It doesn't print <b?palash because it has special characters in it.
If you want to print string with special characters then you should use htmlentities function: print htmlentities($str);
It does work, but your web browser interprets it as a broken HTML tag so it doesn't know what to do with your malformed HTML.
Try pressing Ctrl + U to view the source code.
Try this:
echo htmlentities("<?palash"); // produces <?palash

How to print "\0025cf" in PHP?

I'm rendering some HTML that uses the pseduo before element to render bullet points by setting the content value
.lst-kix_mnfdzhrfoeyd-0>li:before {
content: "\0025cf";
}
But when I'm printing this in PHP the string is truncated to 5cf which means the bullet points won't render.
I've tested this with a simple PHP script and can confirm it happens. Any suggestions? As you can see from the var_dump the preceding character is present as the character count is 4.
<?php
$s = "\0025cf";
print $s;
// 5cf
var_dump($s);
// string(4) "5cf"
Update:
I wrongly put single quotes in the PHP example, but was infact using double-quotes. Having changed the assignment of the string the necessary HTML to use a string literal there is no need to escape the characters. As pointed out in the comments.
Thanks.
If you want to literally output \0025cf then it's as easy as echo "\\0025cf";
If you want to output the character represented by that code, it's not as easy. If you're outputting to HTML context, then try echo "●";.
try this
$s = "\\0025cf";
echo $s;

php strings with <>s in them seem to need escaping

I've got this down to a two line php file:
$s = "<option value=\"%id%\">%desc%</option>";
die($s);
This will output:
%desc%
If I change the <>s to & l t ; & g t ;
then it works but the string I am trying to output is to be interpreted as HTML.
I don't see anything in the PHP string docs to indicate that <>s are special characters that need escaping.
Funny thing is, I have the same problem happening when trying to quote the problem in this forum!
Whats the deal?
You are getting the correct output to your browser, that is
<option value=\"%id%\">%desc%</option>
However, your browser is then parsing this as HTML. You can confirm this by viewing the raw HTML source.
If you do not want your browser to parse it as HTML, use < and >.

PHP Ampersand in String

I'm having a bit of a problem. I am trying to create an IRC bot, which has an ampersand in its password. However, I'm having trouble putting the ampersand in a string. For example...
<?php
$var = "g&abc123";
echo $var;
?>
I believe this should print g&abc123. However it's printing g.
I have tried this as well:
<?php
$arr = array("key" => "g&abc123");
print_r($arr);
?>
This prints it correctly with the g&abc123, however when I say echo $arr['key']; it prints g again. Any help would be appreciated. I'm running PHP5.3.1.
EDIT: Also, I just noticed that if I use g&abc123&abc123 it prints g&abc123. Any suggestions?
I don't have that issue in a console:
php > $d="g&abc123";
php > echo $d;
g&abc123
What environment are you printing the output to? It sounds like you are viewing it in a web browser, and the & is being interpreted as a malformed HTML entity. Try replacing the & symbol with the entity encoded version &.
Look at the source code, it will be printing the correct code.
If you want it to print out correctly in HTML, then run htmlentities on it or make the & &
View the web page source to make sure your variable contains the correct value.
You're probably sending your output to a Web browser.
The correct way of doing it is
In HTML, XHTML and XML, the ampersand has a special meaning. It is used for character entities. You can think of it as an escape sequence of sorts.
For instance, in PHP, this would be illegal:
$variable = 'It's Friday';
This is because the apostrophe is interpreted by PHP as the end of your string, and the rest of your content looks like garbage.
Instead, you have to say:
$variable = 'It\'s Friday';
Similarly, in HTML and XHTML, you can't say
<h1>Inequalities</h1>
<p> x<yz+3 </p>
This is because it would be interpreted as an element.
Instead, you'd have to say:
<h1>Inequalities</h1>
<p> x<yz+3 </p>
Now, as you can see, the ampersand itself has a special meaning and, therefore, needs to be escaped as &. htmlspecialchars() will do it for you.

Categories