Issue
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?)
Solution
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);
Answered By – Rizier123
Answer Checked By – Katrina (BugsFixing Volunteer)