Hướng dẫn php strip_tags vs htmlspecialchars

So I'm wanting to combine the functionality of htmlspecialchars and strip_tags.

Nội dung chính

  • Cùng chuyên mục:
  • Definition and Usage
  • Parameter Values
  • Technical Details
  • More Examples
  • Remove all HTML tags from PHP string with content!
  • Not the answer you're looking for? Browse other questions tagged php or ask your own question.
  • How can we remove the HTML tags from the data in PHP?
  • How can we remove the HTML tags from the data?
  • Which function is used to remove all HTML tags from a string passed to A from?
  • How do I remove HTML tag from string in Wordpress?

The elements I have so far are:

....
....

This is then submitted

....
$content = $_POST['content'];

...Some SQL...

$content = $c['content'];
$new = htmlspecialchars($content, ENT_QUOTES);

I wanting to combine htmlspecialchars and strip_tags together, they both work separately and produce the right result.

$new = strip_tags($content, '');
....

So and example would be that this tag:

would be displayed as plain text where as this tag:


would be formatted correctly.

Hàm strip_tags() sẽ loại bỏ các thẻ HTML và PHP ra khỏi chuỗi. Hàm sẽ trả về chuỗi đã loại bỏ hết các thẻ HTML và PHP.

Nội dung chính

  • Cùng chuyên mục:
  • Definition and Usage
  • Parameter Values
  • Technical Details
  • More Examples
  • Remove all HTML tags from PHP string with content!
  • Not the answer you're looking for? Browse other questions tagged php or ask your own question.
  • How can we remove the HTML tags from the data in PHP?
  • How can we remove the HTML tags from the data?
  • Which function is used to remove all HTML tags from a string passed to A from?
  • How do I remove HTML tag from string in Wordpress?

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Cú pháp

Cú phápstrip_tags( $str, $option);

Trong đó:

  • $str là chuỗi cần loại bỏ cá thẻ HTML và PHP.
  • $option là các thẻ mà bạn muốn hàm strip_tags() không loại bỏ. Các thẻ này sẽ được giữ lại trong chuỗi trả về.

Ví dụ

Code

$str = 'This is a  Example String

this a other string

'; echo $str; echo strip_tags($str) . "
";

Kết quả

This is a Example String
this a other string

This is a Example String this a other string

Code

$str = 'This is a  Example String

this a other string

'; echo $str; echo strip_tags($str, '

');

Kết quả

This is a Example String
this a other string

This is a Example String
this a other string

Tham khảo: php.net

Bài viết này được đăng tại [free tuts .net]

Cùng chuyên mục:

❮ PHP String Reference

Nội dung chính

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

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

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

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 badges200 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

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

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

$string = 

Awesome

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

Try this

Bhargav Rao

47k27 gold badges121 silver badges136 bronze badges

answered Dec 5, 2020 at 12:28

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

How can we remove the HTML tags from the data in PHP?

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.

How can we remove the HTML tags from the data?

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.

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.

How do I remove HTML tag from string in Wordpress?

wp_strip_all_tags is a built in wordpress function. Which is used to strip out tags from the given strings. It is a modified function of PHP strip_tags function or an extended version. strip_tags function is used to remove HTML and PHP tags from strings.