我们在日常php开发中,有时需要正则匹配某个html标签之间的内容,这里记录一下php方法


/**
 * @param $string 需要匹配的字符串
 * @param $tag html标签
 */
function getTagValue($string, $tag){
    $pattern = "/<{$tag}>(.*?)<\/{$tag}>/s";
    preg_match_all($pattern, $string, $matches);
    return isset($matches[1]) ? $matches[1] : '';
}

评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注