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);".
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); ?>
##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)
Description | |
---|---|
string | Required . Specifies the string to check.|
allow | Optional. Specifies allowed tags. These tags will not be deleted.
<?php $str="<s>Hello</s><br> <b>world!</b>"; echo strip_tags($str); ?>
<?php echo strip_tags("Hello <b><i>world!</i></b>","<b>"); ?>
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!