$test = array('<h1>text1</h1>','<h1><a href="#">text2</a><h1>','<h1>text3</h1><p>subtext3</p>');
In a long long texts, I use preg_split cut them into small pieces. I want to remove only h1 tag wraped and without hyperlink.
I hope remove all the text looks like: <h1>text1</h1> //only h1 wraped and without hyperlink.
And remain <h1><a href="#">text2</a><h1>,<h1>text3</h1><p>subtext3</p>
Use a loop to go through each array element and find each instance of the string “<“. Then look at the next 3 characters. If they’re “h1>” then you you have the correct tag. If you ever find a “<” that has a different 3 characters, then its not an “” HTML tag and you can remove this array object.
To remove the given object from the array, you can use unset($array[$index]) and when you’re done I recommend using a sort to remove any index skips that may occur.
You’ll want to use functions such as
strposto get the position of a string, andsubstrto get a subset of the given string. php.net is your friend 🙂Here is an example function which works with your $test array: