Preg_match_all前后添加字符
Preg_match_all add character before and after
我正在使用 preg_match_all
方法从页面上的锚标记内获取 url。它有效但是当我得到它们时,在将它们添加到数组之前我想用 '
包装它们(像这样 'url'
):
preg_match_all('!<a href="(.*?)">!', $anchors, $urls);
有办法吗?如果是,你能指出正确的方向和正确的方法吗?
谢谢! :D
而不是使用 regex to parse html you could use DOMDocument and getElementsByTagName
$dom = new DOMDocument;
$dom->loadHTMLFile("yourfile.html");
$anchors= $dom->getElementsByTagName("a");
$hrefs = [];
foreach ($anchors as $anchor) {
if ($anchor->hasAttribute("href")) {
$hrefs[] = "'{$anchor->getAttribute('href')}'";
}
}
我正在使用 preg_match_all
方法从页面上的锚标记内获取 url。它有效但是当我得到它们时,在将它们添加到数组之前我想用 '
包装它们(像这样 'url'
):
preg_match_all('!<a href="(.*?)">!', $anchors, $urls);
有办法吗?如果是,你能指出正确的方向和正确的方法吗?
谢谢! :D
而不是使用 regex to parse html you could use DOMDocument and getElementsByTagName
$dom = new DOMDocument;
$dom->loadHTMLFile("yourfile.html");
$anchors= $dom->getElementsByTagName("a");
$hrefs = [];
foreach ($anchors as $anchor) {
if ($anchor->hasAttribute("href")) {
$hrefs[] = "'{$anchor->getAttribute('href')}'";
}
}