preg_用锚文本替换href锚


preg_replace href anchor with anchor text

如何用每个锚文本替换所有锚。我的代码是

$body='<p>The man was <a href="http://www.example.com/video/">dancing like a little boy</a> while all kids were watching ... </p>';

我希望结果是:

<p>The man was dancing like a little boy while all kids were watching ... </p>

i used:

$body= preg_replace('#<a href="https?://(?:.+'.)?ok.co.*?>.*?</a>#i', '$1', $body);

,结果是:

<p>The man was while all kids were watching ... </p>

试试这个

$body='<p>The man was <a href="http://www.example.com/video/">dancing like a little boy</a> while all kids were watching ... </p>';
    echo preg_replace('#<a.*?>([^>]*)</a>#i', '$1', $body);

无正则表达式.....

<?php
$d = new DOMDocument();
$d->loadHTML('<p>The man was <a href="http://www.example.com/video/">dancing like a little boy</a> while all kids were watching ... </p>');
$x = new DOMXPath($d);
foreach($x->query('//a') as $anchor){
    $url = $anchor->getAttribute('href');
    $domain = parse_url($url,PHP_URL_HOST);
    if($domain == 'www.example.com'){
        $anchor->parentNode->replaceChild(new DOMText($anchor->textContent),$anchor);
    }
}
function get_inner_html( $node ) {
    $innerHTML= '';
    $children = $node->childNodes;
    foreach ($children as $child) {
        $innerHTML .= $child->ownerDocument->saveXML( $child );
    }
    return $innerHTML;
}
echo get_inner_html($x->query('//body')[0]);

可以使用以下代码:

regex: /< a.*?>|<a.*?>|<'/a>/g

$body='<p>The man was <a href="http://www.example.com/video/">dancing like a little boy</a> while all kids were watching ... </p>';
echo preg_replace('/< a.*?>|<a.*?>|<'/a>/', ' ', $body);

test and show example match word: https://regex101.com/r/mgYjoB/1

您可以在这里简单地使用strip_tags()和htmlspecialchars()

strip_tags -从字符串中删除HTML和PHP标签

htmlspecialchars -将特殊字符转换为HTML实体

步骤1:使用strip_tags()剥离除<p>标签外的所有标签。

第2步:由于我们需要获得字符串和HTML标记,我们需要使用htmlspecialchars()

echo htmlspecialchars(strip_tags($body, '<p>'));

当已经有一个内置的PHP函数时,我认为使用它比使用preg_replace

更好,更紧凑