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"));
Related
Given the following statements:
echo 'string1'."\n";
echo 'string2';
The following gets rendered as output:
string1
string2
With these statements
echo 'string1'.'\n';
echo 'string2';
This gets rendered (note the verbatim backslash n):
string1\nstring2
When \n is in double quotes, it makes a new line as it should.
when \n is in single quotes, it will be shown in the browser as text.
Can anyone explain this behavior?
Using single quotes will mark it as a string, so PHP will literally output \n.
See here: PHP Manual
Alternatively use chr() with the ASCII code of a new line as an argument:
echo 'string'.chr(10);
Or use the <br/> Tag
echo 'string<br/>';
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double
If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters
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
I have some HTML. In my HTML, there is a form that uses a PHP script. When I submit this form, it sends me to a blank page. I have tried this, as suggested in a similar question, but it doesn't work because I have HTML events that call functions with arguments, so both " and ' are already used... I am new to PHP, so, is a there a solution to this that I do not know of?
Use the backslash character \ to escape your quotes like so \". The backslash causes PHP to treat the quote as a regular character. :]
Example:
<?php echo "Hello World"; ?>
outputs Hello World
<?php echo "\"Hello World\""; ?> outputs "Hello World"
Escape your quotes.
echo "<h3>First</h3>"
\" is an escaped quote. When a quote is escaped, the environment treats it as an ordinary character, not as a quote. \' Single quotes can also be escaped if need be.
(Not the best example since I'm not also using singles, but you get the point.)
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>";
I want to display text on the page, the text should look like this:
<sometext> ... but when I echo this, nothing appears!!
How ca I do this?
A "page" is written in HTML, so < means "Start a tag".
You have to represent characters with special meaning in HTML using entities.
You can write them directly, or make use of the htmlspecialchars function.
echo "<sometext>";
echo htmlspecialchars("<sometext>");
You probably want <sometext>.
If that text is coming from user input, you should definitely use htmlspecialchars() on it, to help prevent XSS.
This is because the browser assumes it is an unknown tag. If you want the browser to show it, use:
echo '<sometext>';
or use the htmlentities function like so:
echo htmlentities('<sometext>');
You need to call htmlentities() to convert the HTML metacharacters into something that will display properly.