从字符串中获取所有 href,然后通过另一种方法替换
Get all hrefs from string but then replace via another method
假设您有一个从 ajax 调用中获得的动态字符串。例如,这是一个响应:
$string = '<div>
<a href="http://somelink" class="possible-class">text</a>
<a href="http://anotherlink">other text</a>
</div>';
如何将字符串中的所有 href url 修改为另一种方法的结果,例如此示例方法:
function modify_href( $href ) {
return $href . '/modified';
}
那么结果字符串是:
$string = '<div>
<a href="http://somelink/modified" class="possible-class">text</a>
<a href="http://anotherlink/modified">other text</a>
</div>';
没有关于您需要的进一步信息,这是其中一种方式。
$string = '<div>
<a href="'.modify_href('http://somelink').'" class="possible-class">text</a>
<a href="'.modify_href('http://anotherlink').'">other text</a>
</div>';
function modify_href( $href ) {
return $href . '/modified';
}
echo $string;
要使用正则表达式匹配调用函数,您可以使用函数 preg_replace_callback http://php.net/manual/en/function.preg-replace-callback.php。类似于:
function modify_href( $matches ) {
return $matches[1] . '/modified';
}
$result = preg_replace_callback('/(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)/', 'modify_href', $string);
我还没有测试过这个,但我认为它应该可以工作。我从这里得到了正则表达式:https://rushi.wordpress.com/2008/04/14/simple-regex-for-matching-urls/
是not recommended to parse html with regex.
您可能会使用 DomDocument and createDocumentFragment
function modify_href( $href ) {
return $href . '/modified';
}
$string = '<div>
<a href="http://somelink" class="possible-class">text</a>
<a href="http://anotherlink">other text</a>
</div>';
$doc = new DomDocument();
$fragment = $doc->createDocumentFragment();
$fragment->appendXML($string);
$doc->appendChild($fragment);
$xpath = new DOMXPath($doc);
$elements = $xpath->query("//div/a");
foreach ($elements as $element) {
$element->setAttribute("href", modify_href($element->getAttribute("href")));
}
echo $doc->saveHTML();
假设您有一个从 ajax 调用中获得的动态字符串。例如,这是一个响应:
$string = '<div>
<a href="http://somelink" class="possible-class">text</a>
<a href="http://anotherlink">other text</a>
</div>';
如何将字符串中的所有 href url 修改为另一种方法的结果,例如此示例方法:
function modify_href( $href ) {
return $href . '/modified';
}
那么结果字符串是:
$string = '<div>
<a href="http://somelink/modified" class="possible-class">text</a>
<a href="http://anotherlink/modified">other text</a>
</div>';
没有关于您需要的进一步信息,这是其中一种方式。
$string = '<div>
<a href="'.modify_href('http://somelink').'" class="possible-class">text</a>
<a href="'.modify_href('http://anotherlink').'">other text</a>
</div>';
function modify_href( $href ) {
return $href . '/modified';
}
echo $string;
要使用正则表达式匹配调用函数,您可以使用函数 preg_replace_callback http://php.net/manual/en/function.preg-replace-callback.php。类似于:
function modify_href( $matches ) {
return $matches[1] . '/modified';
}
$result = preg_replace_callback('/(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)/', 'modify_href', $string);
我还没有测试过这个,但我认为它应该可以工作。我从这里得到了正则表达式:https://rushi.wordpress.com/2008/04/14/simple-regex-for-matching-urls/
是not recommended to parse html with regex.
您可能会使用 DomDocument and createDocumentFragment
function modify_href( $href ) {
return $href . '/modified';
}
$string = '<div>
<a href="http://somelink" class="possible-class">text</a>
<a href="http://anotherlink">other text</a>
</div>';
$doc = new DomDocument();
$fragment = $doc->createDocumentFragment();
$fragment->appendXML($string);
$doc->appendChild($fragment);
$xpath = new DOMXPath($doc);
$elements = $xpath->query("//div/a");
foreach ($elements as $element) {
$element->setAttribute("href", modify_href($element->getAttribute("href")));
}
echo $doc->saveHTML();