这将是您的设置:
function get_tags($string, $start, $end)
{
$start = str_replace("\\", "\\\\", $start);
$start = str_replace("/", "\/", $start);
$end = str_replace("\\", "\\\\", $end);
$end = str_replace("/", "\/", $end);
preg_match_all("/{$start}(.*?){$end}/si", $string, $matching_data);
return $matching_data[0];
}
function return_between($string, $start, $stop, $type)
{
$temp = split_string($string, $start, false, $type);
return split_string($temp, $stop, true, $type);
}
function get_attribute($tag, $attribute)
{
// Remove all line feeds from the string
$cleaned_html = str_replace("\r", "", $tag);
$cleaned_html = str_replace("\n", "", $cleaned_html);
// Use return_between() to find the properly quoted value for the attribute
return return_between($cleaned_html, $attribute."=\"", "\"", true);
}
要使用它,像这样:
$open_tag = '<span';
$close_tag = '>';
$span_tags = get_tags($html_string, $open_tag, $close_tag);
$span_tag_class_names = array();
foreach ($span_tags as $key => $tag) {
$class_name = get_attribute($tag, $attribute = "class");
if (!empty($class_name)) {
$span_tag_class_names[] = $class_name;
}
}
print_r($span_tag_class_names);
与所有正则表达式一样,您的里程可能会有所不同。