110

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?

1
  • sample input and output......... Commented Feb 4, 2013 at 9:48

10 Answers 10

179

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) . "..."; ?>
Sign up to request clarification or add additional context in comments.

4 Comments

Why not works ? :( I'm using : data = htmlentities($description2,ENT_QUOTES, 'UTF-8'); , strip_tags($data) and not works
would this secure the page from xss scripting attacks too?
@delive Why in the world would you run htmlentities and then strip_tags? That totally defeats the purpose.
It might not block certain kinds of JavaScript. Don't expect miracles from this function.
25

Use PHP's strip_tags() function.

For example:

$businessDesc = strip_tags($row_get_Business['business_description']);
$businessDesc = substr($businessDesc, 0, 110);


print($businessDesc);

2 Comments

This will first take the 100 characters and after that remove the html tags. But I think OP wants to first remove html tags and after that substr 100 characters.
@YogeshSuthar Very true, I'll edit the answer now, thank you for pointing that out. - Done
17

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.

1 Comment

Almost. Section 12.1.2.2.4 of WhatWG says </script > is valid end tag, but is not handled by the regex. Should be </\1\s*> or some such.
8

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

5 Comments

strip_tags is there, why regex?
Not completely sure, but I think it won't catch self closing tags that contain white space: <br /> or <special />. I also think that this doesn't take hacks like <b<b>></</b>> into account.
I think kaiser was referring to this answer, not strip_tags
This is better solution than PHP strip_tag. PHP strip_tag will remove both opening and closing HTML Script Element. However, if your user puts only the opening HTML Script Element then PHP strip_tag will not remove it. Then your web page will very likely display utterly wrong. Tested with PHP version 5.6.19. This little regex fixed those partial HTML tags that can cause problems that strip_tag will miss. Bravo!
The problem is that sometimes the user will write invalid html, so for example, <div Name of the client <div>hello></div> , and suing strip_tag will remove everything... and somes we want a more preserve way, so i would go with regex... "Because strip_tags() does not actually validate the HTML, partial or broken tags can result in the removal of more text/data than expected."
4

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>");
?>

Comments

3

For me, this is the 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; 

}

Comments

1

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)}}

Comments

1

<?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 } ?>

Comments

1
$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

Comments

-1

If you are using the code in DataTable, it might not work. Try this instead:

<?php echo strip_tags(html_entity_decode(<p>Hello</p> <b><i>world!</i></b>)); ?>

1 Comment

The string passed to html_entity_decode() has no quotes. It doesn't work: 3v4l.org/aBEBS

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.