How to remove html tag from string in php

❮ PHP String Reference

Example

Strip the string from HTML tags:

echo strip_tags("Hello world!");
?>

Try it Yourself »


Definition and Usage

The strip_tags() function strips a string from HTML, XML, and PHP tags.

Note: HTML comments are always stripped. This cannot be changed with the allow parameter.

Note: This function is binary-safe.


Syntax

Parameter Values

ParameterDescription
string Required. Specifies the string to check
allow Optional. Specifies allowable tags. These tags will not be removed

Technical Details

Return Value:Returns the stripped string
PHP Version:4+
Changelog:As of PHP 5.3.4, this function ignores self-closing XHTML tags (like
) in allow parameter
As of PHP 5.0, this function is binary-safe.
As of PHP 4.3, HTML comments are always stripped.

More Examples

Example

Strip the string from HTML tags, but allow tags to be used:

echo strip_tags("Hello world!","");
?>

Try it Yourself »


❮ PHP String Reference


I want to display the first 110 characters of a database entry. Pretty easy so far:


But the above entry has html code in it that's been entered by the client. So it displays:

Ref no: 30001

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?

j0k

22.3k28 gold badges77 silver badges86 bronze badges

asked Feb 4, 2013 at 9:47

1

use strip_tags

$text = '

Test paragraph.

Other text'; echo strip_tags($text); //output Test paragraph. Other text

answered Feb 4, 2013 at 9:48

How to remove html tag from string in php

Yogesh SutharYogesh Suthar

30.2k18 gold badges70 silver badges98 bronze badges

4

Use PHP's strip_tags() function.

For example:

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


print($businessDesc);

answered Feb 4, 2013 at 9:48

How to remove html tag from string in php

EM-CreationsEM-Creations

4,0274 gold badges38 silver badges55 bronze badges

2

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 = 'Some Text
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.*?>.*?@si', '', $text);
    
 }

Output:

Lorem Ipsum is simply dummy text of the printing and typesetting industry.

answered Sep 4, 2016 at 18:26

How to remove html tag from string in php

Muhammad ShahzadMuhammad Shahzad

8,79021 gold badges79 silver badges128 bronze badges

1

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

answered Feb 4, 2013 at 9:50

Maxim ShoustinMaxim Shoustin

78.2k28 gold badges201 silver badges222 bronze badges

5

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; 

}

answered Aug 7, 2019 at 8:08

How to remove html tag from string in php

David G.David G.

211 silver badge3 bronze badges

Strip the string from HTML tags:

world!");
?>

Strip the string from HTML tags, but allow tags to be used:

world!","");
?>

answered Apr 18, 2021 at 9:53

How to remove html tag from string in php

lookly Devlookly Dev

3073 silver badges4 bronze badges

In laravel you can use following syntax

 @php
   $description='

Rolling coverage

' @endphp {{ strip_tags($description)}}

answered Dec 10, 2018 at 17:18

Welcome to my PHP class, we are glad you are here

"; echo strip_tags($data); ?>

Or if you have a content coming from the database;

100) { ?>...

answered Sep 29, 2020 at 9:39

How to remove html tag from string in php

$string = 

Awesome

Website by Narayan. Thanks for visiting enter code here; $tags = array("p", "i"); echo preg_replace('#<(' . implode( '|', $tags) . ')(?:[^>]+)?>.*?#s', '', $string);

Try this

How to remove html tag from string in php

Bhargav Rao

47.1k27 gold badges121 silver badges136 bronze badges

answered Dec 5, 2020 at 12:28

How to remove html tag from string in php

Not the answer you're looking for? Browse other questions tagged php or ask your own question.

How do I remove a specific HTML tag from a string in PHP?

PHP provides an inbuilt function to remove the HTML tags from the data. The strip_tags() function is an inbuilt function in PHP that removes the strings form HTML, XML and PHP tags. It accepts two parameters. This function returns a string with all NULL bytes, HTML, and PHP tags stripped from a given $str.

How do I remove a tag from a string?

The HTML tags can be removed from a given string by using replaceAll() method of String class. We can remove the HTML tags from a given string by using a regular expression. After removing the HTML tags from a string, it will return a string as normal text.

What is strip HTML?

stripHtml( html ) Changes the provided HTML string into a plain text string by converting
,

, and

to line breaks, stripping all other tags, and converting escaped characters into their display values.

Which function is used to remove all HTML tags from a string passed to A from?

Which function is used to remove all HTML tags from a string passed to a form? Explanation: The function strip_tags() is used to strip a string from HTML, XML, and PHP tags.