php 正则匹配html标签之间内容

标签:,
来源: 腾讯云Qcloud评测
日期: 2021-3-26
作者: 腾讯云/服务器VPS推荐评测/Vultr

我们在日常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] : '';
}

链接到文章: https://qcloudx.com/47028.html

发表回复

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