How can I join '<' and 'div>'?
When I echo
<?php echo "<"."div>";?>
it returns an empty string.
Because it is a HTML Tag. You cannot print it like this.
Still if you want to print it you can see #egig's suggestion. You can use echo "<"."div>"
That's because you're echoing a HTML tag. View the source of the page.
use:
htmlspecialchars("<a href='test'>Test</a>");
to display html tags in php
You can use htmlspecialchars for this.
<?php echo htmlspecialchars("<"."div>"); ?>
Your code is executed and the '<' is joined with the 'div>', however there is nothing to show as you are trying to output a partial HTML tag in PHP. You can do this by means of entity code:
< Less-than <
> Greater-than >
In your case it looks like:
echo "<"."div>";
Other means of doing this is using PHP to add these entity codes for you:
htmlentities — convert all applicable characters to HTML entities
In code:
echo htmlentities('<div>');
Make sure you use echo, or otherwise you would end up with an empty string again.
Other means:
htmlspecialchars — convert special characters to HTML entities
In code:
echo htmlspecialchars('<div>');
Related
I'm trying to print out some html code which isn't stored as a simple string so I need to decode it before echo-ing, my problem is when I echo a decoded value I keep getting these quotes and they are ruining the output, this is how it looks:
<div>
"<h1 class="ql-align-center">TEST</h1>"
</div>
so because of these quotes " h1 failes to form and it prints out as a text and not as an html code.
So i'm wondering is it possible to print as html code meaning there is no quotes "" ?
this is the php code that it generates it like this
<?php echo html_entity_decode($singleEmail['camp_desc'], ENT_NOQUOTES, 'UTF-8'); ?>
also this is the database value of 'camp_desc' that has to be encoded before being printed
<h1 class="ql-align-center">TEST</h1>
and the output of php code above for encode is
<h1 class="ql-align-center">TEST</h1>
but since I'm using echo to print.... php wraps it with quotes and <h1> tag becomes a plain text instead of html element
I don't know where the quotes are coming from - the code you have in your question doesn't add extra quotes so they are coming from somewhere else.
However if you want the HTML string to be rendered as HTML instead of displaying the tags as text, you can do the following:
Starting with this value in your variable:
<h1 class="ql-align-center">TEST</h1>
displayed as: <h1 class="ql-align-center">TEST</h1>
...you can use html_entity_decode to decode it which will give us the following output, i.e. it converts it into a string that will display as plain text HTML when you echo it:
<h1 class="ql-align-center">TEST</h1>
displayed as: <h1 class="ql-align-center">TEST</h1>
...now we need to decode this to turn it into the HTML elements that will be displayed as a H1 tag in the page:
<h1 class="ql-align-center">TEST</h1>
displayed as: TEST
Code: To do this, you need to call html_entity_decode twice before it will display the string as HTML elements:
<?php
$htmlstr = html_entity_decode($singleEmail['camp_desc'], ENT_QUOTES, 'UTF-8');
echo html_entity_decode($htmlstr, ENT_NOQUOTES, 'UTF-8');
?>
What if you try to replace those quotes when echo-ing them?
Like you make regular expression to replace it or you make a function that replaces the two parts you want like str_replace('”<', '<', $yourDecodedHtml),
str_replace('>”', '>', $yourDecodedHtml)
I want to remove quotes and html tags from following string:
$mystring='Example string "I want to remove any html tags ABC<sub>DE</sub> from <p>similar</p> types of string?"';
I am using following scripts to remove it but it's doesn't work for me:
echo strip_tags(htmlentities($mystring,ENT_QUOTES));
I want to following output for above string:
Example string "I want to remove any html tags ABCDE from similar types of string?"
Why strip is not working?What I mess here?
Once you use htmlentities() on that string there are no tags left to strip because they have been converted, well, to their HTML entities.
At first use strip_tags function if you want remove html tags then use htmlentities as follow it should be work:
echo htmlentities(strip_tags($mystring),ENT_QUOTES);
Can you use this code
echo strip_tags($mystring);
So i'm trying to get my nl2br to work but it just wont work the way i hope it would.
As it retrieves text from a database, i want it to recognise the \n with the nl2br I have htmlspecialchars which is the reason it doesn't work. How would i solve this issue?
My code:
The post variable looks like this
$post = nl2br($row['p_post']);
Which means that it ends up inside the htmlspecialchars()
echo '<p>'.htmlspecialchars($post, ENT_QUOTES).'</p>;
I tried to put it like this echo '<p>'.nl2br(htmlspecialchars($post, ENT_QUOTES)).'</p>; but that ended up looking as if there was no nl2br at all.
How it looks from the start
How it looks after i've added the nl2br
You're double encoding the tags:
$foo = "how\ndoes\nthis\nlook";
$post = nl2br($foo); // $post is now "how<br>does<br>this<br>look"
$temp = nl2br(htmlspecialchars($post)); // $temp is now "how<br>does etc..."
echo '<p>how<br>does< etc...';
You converted the line breaks to break tags, then you html encoded the break tags to html character entities, so your browser renders those tags as "visual" tags, and not HTML line breaks at all.
This was fixed, it seemed that another variable collided with it somehow so that it wouldn't actually make the <br> tags.
I'm trying to put html into a mailto link (<a href='mailto:...) and using htmlescapechars() but this doesn't seem to be what I need.
<a href='mailto:?subject=<?echo $subject_e=htmlspecialchars($subject, ENT_QUOTES)?>&body=<?=$subject_e?>%20via%20https://mySite.com/<?=htmlspecialchars($subjectLink, ENT_QUOTES)?>%20'>some text</a>
Can anyone explain under what conditions Example #1 in the htmlspecialchars() part of PHP's documentation works?
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
Running this function on my own server or on http://writecodeonline.com/php/ doesn't seem to convert the special characters.
So it doesn't appear to be a PHP version issue. Including the encoding parameter also had no effect. Wtf?
Running this function on my own server [...] doesn't seem to convert the special characters.
I bet you’re just thinking that because you view the script output as HTML … and what does < display as in HTML? Right, a < character …
You're looking at it wrong! The reason you're seeing the HTML tags IS because the magic php function is working. Otherwise, it would just render as actual HTML. This is happening because you're likely printing on to an HTML page.
So after looking elsewhere on SO, I realized instead of htmlspecialchars() I just needed rawurlencode().
It actually IS working. If you view the source, you will see it is working properly. The HTML is rendering the special chars though
If you look at the code of your page you'll see what was generated was exactly what was expected. Loo:
<div class="page-body">
<div id="code-output" class="code-output"><a href='test'>Test</a></div>
</div>
The problem is that when you render the page, the html entities are converted to text again. If it's not what you want, you should put the output inside <pre> tags.
How can stop HTML code from rendering and display it as standard text, so my users can simply copy and paste it for their own usage?
You could use the function htmlentities(). Have a look at the manual.
Please note that by default the data will be returned using the ISO-8859-1 character set. You might want to change the third parameter to UTF-8.
For example:
$html = "<div>Some text</div>";
echo htmlentities($html);
Use PHP to set the content type:
header("Content-Type: text/plain");
You parse it into markup which uses HTML entities, for example instead of using:
<body>
you would instead use entity codes to escape the markup characters, like this:
<body>
There are plenty of references to be found which list entity codes, and it's pretty easy to parse and escape HTML in most programming languages.
HTML code can be displayed using php script by using functions htmlentities() and htmlspecialchars(). For understanding this concept consider the following example:
<?php
$a="<h1>heading tag: used for heading tags</h1>";
echo htmlentities($a)."<br/>";
echo htmlspecialchars($a);
?>
Output will be:
<h1>heading tag: used for heading tags</h1>
<h1>heading tag: used for heading tags</h1>