$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 with开发者_如何学Pythonout 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 strpos
to get the position of a string, and substr
to get a subset of the given string. php.net is your friend :)
Here is an example function which works with your $test array:
<?php
$test = array('<h1>text1</h1>','<h1><a href="#">text2</a><h1>','<h1>text3</h1><p>subtext3</p>');
function removeBadElements(&$array) {
foreach($array as $k => $v) {
// $v is a single array element
$offset = 0;
do {
$pos = strpos($v, '<', $offset);
$offset = $pos + 1;
if($pos === false) { break; }
$tag = substr($v, $pos, 3);
$next = substr($v, $pos+1, 1);
if($next == '/') { continue; }
if($tag == '<h1') { continue; }
else {
unset($array[$k]);
break;
}
} while($offset + 2 < strlen($v));
}
}
echo "\nORIG ARRAY:\n";
print_r($test);
removeBadElements($test);
echo "\n\n-------\nMODIFIED ARRAY:\n\n";
print_r($test);
?>
精彩评论