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
Related
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;
echo preg_quote("aaa<bbb");
should write:
aaa\<bbb
but I get:
aaa\
This is the only sign that makes problems.
If you want to display it in browser what it just is, you could wrap it in <pre> tag.
echo '<pre>'.preg_quote("aaa<bbb").'</pre>';
Or you could use htmlspecialchars to escape the <.
echo htmlspecialchars(preg_quote("aaa<bbb"));
PHP is echoing JavaScript (I'm using the jQuery library) something like this:
echo 'var users = $("#add").val().split("\n");';
However, the \n is creating a line break in what the echoed script looks like, and therefore breaking the JavaScript. Is there a way to circumvent this?
Many thanks!
The \n is an escape sequence meaning newline. Backslashes are the beginning of escape sequences, to output a backslash then write \\. So you want \\n. Other useful escape sequences include the quote: use \" to put a quote into the string instead of ending the string.
echo "var users = $(\"#add\").val().split(\"\\n\");";
Not sure If you looking for this
echo "<script>alert('Line1\\\\nThis still in Line1')</script>";
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);
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.