php to display text as html formated stored in database - php

I have one text saved in MySQL Database
<p> Celebrate with these amazing<br /> offers direct from the
Now when I print that text using
echo
I got this output
<p> Celebrate with these amazing<br /> offers direct from the
but U want to display that as
Celebrate with these amazing
offers direct from the
like HTML print.
when i see in db its stored like bellow
<p>
Celebrate with these amazing<br />
offers from
How to do this?

Assuming your database has saved the escaped information like this:
<p>
Celebrate with these amazing<br />
offers from
Then you could just use PHP's html_entity_decode function to output that block of HTML.

maybe this could help you http://php.about.com/od/phpwithmysql/qt/php_in_mysql.htm

If you are outputting to the console (it's not clear from your question what you are trying to output to):
If you look at the PHP strip_tags documentation, you should be able to reach something approximating what you want. http://uk3.php.net/manual/en/function.strip-tags.php
With strip_tags(), you can allow the tag to remain and then replace it with "\n" afterwards.
Otherwise, if you are outputting to browser, then other posts on this page have the answer - you must be storing the htmlentities() version of the data in the database.

Related

PHP not reflecting desired HTML

I'm making a website where a user should be able to use HTML and CSS in their profiles but I came across one problem.
<?php
$profile = "<h1>THIS IS A TEST</h1>";
echo htmlentities($profile);
?>
That's my code, but it only show this in the profile:
<h1>THIS IS A TEST&amp</h1>
I don't know what is happening, nor do I know if this only happens to me.
How do I make it show only the h1 content?
Function htmlentities is showing the representation of html characters like tags etc., and is being used especially to avoid parsing as html. So if you mean to echo html so that the browser parses it as html, the last thing you want is to use this function! Just echo it out directly, no need to use htmlentities or htmlspecialchars!
You just have to use echo $profile;, that's all. Check this and don't forget to check Display as HTML as browsers display PHP echoed text as HTML unless they're told to display it differently.

How to extract only text from HTML string with PHP?

I want to extract only text from a php string.
This php string contains html code like tags or etc.
So I only need a simple text from this string.
This is the actual string:
<div class="devblog-index-content battlelog-wordpress">
<p><strong>The celebration of the Recon class in our second </strong>BF4 Class Week<strong> continues with a sneaky stroll down memory lane. Learn more about how the Recon has changed in appearance, name and weaponry over the years…</strong></p>
<p> </p>
<p style="text-align:center"><img alt="bf4-history-of-recon-1" class="aligncenter" src="http://eaassets-a.akamaihd.net/battlelog/prod/954660ddbe53df808c23a0ba948e7971/en_US/blog/wp-content/uploads/2014/10/bf4-history-of-recon-1.jpg?v=1412871863.37" style="width:619px" /></p>
I want to show this from the string:
The celebration of the Recon class in our second BF4 Class Week continues with a sneaky stroll down memory lane. Learn more about how the Recon has changed in appearance, name and weaponry over the years…
Actually this text will be placed in meta description tag so I don't need any HTML in meta tag.
How can I perform this? Any ideas and thoughts about this technique ?
You may try:
echo(strip_tags($your_string));
More info here: http://php.net/manual/en/function.strip-tags.php
Another option is to use Html2Text. It will do a much better job than strip_tags, especially if you want to parse complicated HTML code.
Extracting text from HTML is tricky, so your best bet is to use a library built for this purpose.
https://github.com/mtibben/html2text
Install using composer:
composer require html2text/html2text
Basic usage:
$html = new \Html2Text\Html2Text('Hello, "<b>world</b>"');
echo $html->getText(); // Hello, "WORLD"
Adding another option for someone else who may need this, the Stringizer library might be an option, see Strip Tags.
Full disclosure I'm the owner of the project.

Text in <textarea> automatically getting tabbed in

pretty new to Mysql, HTML and PHP and I can't seem to find much information on this trouble i'm having.
I Am making my own rough project manager type thing and I have a form that lets me change the contents of each individual change log, the problem I have however is that when I load the data in to the text area it start with a big indentation at the start, like 3-4 tabs inwards. I would attach an image but I need at least 10 rep to do that.
Basically, it feels like the data in the database has tabs or something at the start of it, but when I go to look in PHPmyadmin at the field, it just looks like it should do, not tabbed at all.
I've tried using strip_tags() but I think it only works on visible tags.
Does any1 know how to get rid of this or what is causing the problem?
I'll be following this question closely to see if anybody can provide an answer because I'm stumped.
Thanks,
Try to echo your php code with no blanks :
Possible tabs, bad example :
<textarea>
<?php echo $tabContent; ?>
</textarea>
Avoiding tabs :
<textarea><?php echo $tabContent; ?></textarea>
You can also try to trim your php content like that :
<textarea><?php echo trim($tabContent); ?></textarea>
Try doing var_dump and look how long your queried string is.
If it is as long as in your database you problem is within the textarea.
Is there any css you use with textarea?

Textarea content to database

I have this textarea called personalInfos where i fill the infos in following format :
<p><span class="white">1966 - '69</span><br/> text .... </p>
When i submit it to database, it gets saved ok, same format. When i retrieve the code from database to admin textarea it gets filled ok.
My only problem is on front end where i get displayed the code as text not rendered as html code. So basiclly i see it on the page like this :
<p><span class="white">1966 - '69</span><br/>
Most likely you display fetched code parsed processed by htmlentities() or similar function. This is in most cases the way to go to avoid planting i.e. html in comments. So simply stop doing this after fetching (or insert - depends where you do so) and your content will be outputed as literaly HTML and properly processed by web browser.
You should have a look at htmlspecialchars_decode()
Example
$str = '<p><span class="white">1966 - \'69</span><br/> text .... </p>';
echo htmlspecialchars_decode($str);
Also make sure to escape the single quotes as well.

Can I use PHP to save only the visible elements of the output as a new file?

I've got a php file that takes an xml file (generated by an outside source) and reformats it with CSS & HTML. A number of the XML tags are things I don't want to see in the final version, so I have them hidden. The end result is something like this:
<html>
<div style="display: none">
content i don't want to see
</div>
content I do want to see.
</html>
Is there a way I can take the resulting html file as it's displayed in the browser window,
content I do want to see.
…and save that as a text file? I want it to ignore all the hidden <div> tags and only save what can otherwise be selected and copied by the user.
I've looked around for an answer to this but I'm not even really sure what I'm looking for or how to search it.
I've also tried this:
ob_start();
file_put_contents('filename.htm', ob_get_contents());
ob_end_flush();
… but that's doesn't solve it. I have a number of tags in the outputted test (> etc) that need to be saves as they are displayed, and ob_get_contents() takes the page's source code, not the displayed version.
This matters because the outputted page is also PHP that has been generated based on other factors, so I need to use html unicode values to keep the $ signs and quotes from messing up the source PHP.
I hope that was clear. Thanks in advance for any help or suggestions.
I think you have to strip out the unneeded part manually, using a RegEx something, which maybe like:
$content_raw = ob_get_contents();
$content_stripped = preg_replace($content_raw, '<div style="display: none">[^<>]*</div>', '');
file_put_contents('filename.htm', $content_stripped);

Categories