Remove all html tags from php string
PhpPhp Problem Overview
I want to display the first 110 characters of a database entry. Pretty easy so far:
<?php echo substr($row_get_Business['business_description'],0,110) . "..."; ?>
But the above entry has html code in it that's been entered by the client. So it displays:
<p class="Body1"><strong><span style="text-decoration: underline;">Ref no:</span></strong> 30001<strong></stro...
Obviously no good.
I just want to strip out all html code, so I need to remove everything between < and > from the db entry THEN display the first 100 chars.
Any ideas anyone?
Php Solutions
Solution 1 - Php
use strip_tags
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text); //output Test paragraph. Other text
<?php echo substr(strip_tags($row_get_Business['business_description']),0,110) . "..."; ?>
Solution 2 - Php
Use PHP's strip_tags() function.
For example:
$businessDesc = strip_tags($row_get_Business['business_description']);
$businessDesc = substr($businessDesc, 0, 110);
print($businessDesc);
Solution 3 - Php
Remove all HTML tags from PHP string with content!
Let say you have string contains anchor tag and you want to remove this tag with content then this method will helpful.
$srting = '<a title="" href="/index.html"><b>Some Text</b></a>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.';
echo strip_tags_content($srting);
function strip_tags_content($text) {
return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text);
}
Output:
>Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Solution 4 - Php
use this regex: /<[^<]+?>/g
$val = preg_replace('/<[^<]+?>/g', ' ', $row_get_Business['business_description']);
$businessDesc = substr(val,0,110);
from your example should stay: Ref no: 30001
Solution 5 - Php
For my this is best solution.
function strip_tags_content($string) {
// ----- remove HTML TAGs -----
$string = preg_replace ('/<[^>]*>/', ' ', $string);
// ----- remove control characters -----
$string = str_replace("\r", '', $string);
$string = str_replace("\n", ' ', $string);
$string = str_replace("\t", ' ', $string);
// ----- remove multiple spaces -----
$string = trim(preg_replace('/ {2,}/', ' ', $string));
return $string;
}
Solution 6 - Php
Strip the string from HTML tags:
<?php
echo strip_tags("Hello <b>world!</b>");
?>
Strip the string from HTML tags, but allow tags to be used:
<?php
echo strip_tags("Hello <b><i>world!</i></b>","<i>");
?>
Solution 7 - Php
In laravel you can use following syntax
@php
$description='<p>Rolling coverage</p><ul><li><a href="http://xys.com">Brexit deal: May admits she would have </a><br></li></ul></p>'
@endphp
{{ strip_tags($description)}}
Solution 8 - Php
<?php $data = "<div><p>Welcome to my PHP class, we are glad you are here</p></div>"; echo strip_tags($data); ?>
Or if you have a content coming from the database;
<?php $data = strip_tags($get_row['description']); ?>
<?=substr($data, 0, 100) ?><?php if(strlen($data) > 100) { ?>...<?php } ?>
Solution 9 - Php
$string = <p>Awesome</p><b> Website</b><i> by Narayan</i>. Thanks for visiting enter code here;
$tags = array("p", "i");
echo preg_replace('#<(' . implode( '|', $tags) . ')(?:[^>]+)?>.*?</\1>#s', '', $string);
Try this