替换 html 文档中的所有标题属性
Replace all title attributes in an html document
我在变量中有 html 代码。例如 $html
等于:
<div title="Cool stuff" alt="Cool stuff"><a title="another stuff">......</a></div>
我需要将所有标题属性title="Cool stuff"
和title="anot stuff"
等的内容替换为title="$newTitle"
。
有什么non-regex方法可以做到这一点吗?
如果我必须使用正则表达式,是否有比我提出的更好的(performance-wise)and/or更优雅的解决方案?
$html = '...'
$newTitle = 'My new title';
$matches = [];
preg_match_all(
'/title=(\"|\')([^\"\']{1,})(\"|\')/',
$html,
$matches
);
$attributeTitleValues = $matches[2];
foreach ($attributeTitleValues as $title)
{
$html = str_replace("title='{$title}'", "title='{$newTitle}'", $html);
$html = str_replace("title=\"{$title}\"", "title=\"{$newTitle}\"", $html);
}
绝对不要使用正则表达式——这是一个肮脏的兔子洞。
...这个洞是肮脏的,不是兔子:)
我更喜欢使用 DomDocument 和 Xpath 直接定位 html 文档中所有元素的所有 title
属性。
LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD
标志已到位,以防止您的输出被 <doctype>
和 <html>
标记修饰。
//
在 XPath 表达式中说:去任何深度搜索匹配项
代码:(Demo)
$html = <<<HTML
<div title="Cool stuff" alt="Cool stuff"><a title="another stuff">......</a></div>
HTML;
$newTitle = 'My new title';
$dom = new DOMDocument();
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//@title') as $attr) {
$attr->value = $newTitle;
}
echo $dom->saveHTML();
输出:
<div title="My new title" alt="Cool stuff"><a title="My new title">......</a></div>
我在变量中有 html 代码。例如 $html
等于:
<div title="Cool stuff" alt="Cool stuff"><a title="another stuff">......</a></div>
我需要将所有标题属性title="Cool stuff"
和title="anot stuff"
等的内容替换为title="$newTitle"
。
有什么non-regex方法可以做到这一点吗?
如果我必须使用正则表达式,是否有比我提出的更好的(performance-wise)and/or更优雅的解决方案?
$html = '...'
$newTitle = 'My new title';
$matches = [];
preg_match_all(
'/title=(\"|\')([^\"\']{1,})(\"|\')/',
$html,
$matches
);
$attributeTitleValues = $matches[2];
foreach ($attributeTitleValues as $title)
{
$html = str_replace("title='{$title}'", "title='{$newTitle}'", $html);
$html = str_replace("title=\"{$title}\"", "title=\"{$newTitle}\"", $html);
}
绝对不要使用正则表达式——这是一个肮脏的兔子洞。
...这个洞是肮脏的,不是兔子:)
我更喜欢使用 DomDocument 和 Xpath 直接定位 html 文档中所有元素的所有 title
属性。
LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD
标志已到位,以防止您的输出被<doctype>
和<html>
标记修饰。//
在 XPath 表达式中说:去任何深度搜索匹配项
代码:(Demo)
$html = <<<HTML
<div title="Cool stuff" alt="Cool stuff"><a title="another stuff">......</a></div>
HTML;
$newTitle = 'My new title';
$dom = new DOMDocument();
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//@title') as $attr) {
$attr->value = $newTitle;
}
echo $dom->saveHTML();
输出:
<div title="My new title" alt="Cool stuff"><a title="My new title">......</a></div>