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

How to remove html tags from array elements in php

青灯夜游
青灯夜游Original
2023-01-07 18:01:192953browse

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) { 
    //循环体代码
}

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

$value=strip_tags($value);

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

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)

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

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

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!

Statement:
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