Home > Backend Development > PHP Problem > How to remove html tags from array elements in php

How to remove html tags from array elements in php

青灯夜游
Release: 2023-03-17 18:10:01
Original
2964 people have browsed it

Implementation steps: 1. Use the foreach statement to traverse the array elements by referencing the loop, with the syntax "foreach ($array as &$value) { //loop body code}"; 2. In the loop body, Use the strip_tags() function to remove the html tags in the array elements, the syntax is "$value=strip_tags($value);".

How to remove html tags from array elements in php

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

php removes the html in the array element Tag method

In PHP, you can use the foreach statement and strip_tags() function to remove html tags from array elements.

Implementation steps: 1. Use the foreach statement to traverse the array elements through reference loops

Generally, when using the foreach statement to traverse the array, it is a backup of the array Operations generally do not affect the array itself.

If you want to affect the array itself, you need to use a reference cycle to modify the array elements (add & before $value), so that the foreach statement will assign a value by reference instead of copying a value.

foreach ($array as &$value) { 
    //循环体代码
}
Copy after login

Step 2: In the loop body, use the strip_tags() function to remove the html tags in the array elements

$value=strip_tags($value);
Copy after login

The loop ends, and the All html tags will be removed.

Implementation example:

<?php
$array= array("1<br>","hello<br>","This is some <b>bold<b> text.","<b>4<b>","<s>5<s>");
var_dump($array);
foreach ($array as &$value) { 
    $value=strip_tags($value);
}
var_dump($array);
?>
Copy after login

How to remove html tags from array elements in php

##Extended knowledge: strip_tags() function

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

Comments: This function always strips HTML comments. This cannot be changed via the allow parameter.

Note: This function is binary safe.

strip_tags(string,allow)
Copy after login

ParametersDescriptionRequired . Specifies the string to check. Optional. Specifies allowed tags. These tags will not be deleted.
string
allow
Return value: Returns the stripped string.

Example 1: Strip all HTML tags in a string:

<?php
$str="<s>Hello</s><br> <b>world!</b>";
echo strip_tags($str);
?>
Copy after login

How to remove html tags from array elements in php

Example 2: Strip all HTML tags in a string, but allow Tag:

<?php
echo strip_tags("Hello <b><i>world!</i></b>","<b>");
?>
Copy after login

How to remove html tags from array elements in php

Recommended study: "

PHP Video Tutorial"

The above is the detailed content of How to remove html tags from array elements in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template