Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove all html tags from an array?

Is there a function in php to do a regex replace kind of action on all entries of an array?
I have an array that contains lots of html tags with text in them and I want to remove the tags.
So basically I'm converting this:

$m = [
"<div>first string </div>",
"<table>
   <tr>
     <td style='color:red'>
       second string
     </td>
   </tr>
 </table>",
"<a href='/'>
   <B>third string</B><br/>
 </a>",
];

to this:

$m = [
"first string",
"second string",
"third string"
]

The regex that (hopefully) matches everything I want to remove, looks like this:

/<.+>/sU

The question is just how I should use it now? (My array actually has more than 50 entries and in every entry there can be like 10 matches, so using preg_replace is probably not the way to go, or is it?)

like image 997
Forivin Avatar asked Sep 16 '15 17:09

Forivin


People also ask

Which function is used to remove all HTML tags?

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.

How do you remove tags in HTML?

Approach: Select the HTML element which need to remove. Use JavaScript remove() and removeChild() method to remove the element from the HTML document.


1 Answers

No need for a regex here, just use strip_tags() to get rid of all html tags and then simply trim() the output, e.g.

$newArray = array_map(function($v){
    return trim(strip_tags($v));
}, $m);
like image 94
Rizier123 Avatar answered Sep 28 '22 18:09

Rizier123