使用 PHP 将 <a> 换成 <span>

Swapping an <a> for <span> using PHP

有人可以帮我编辑我的问题,使其符合 SO 规则吗?我提出了一个有效的问题并从一个有用的 SO'er 那里得到了答案,但它还没有被 SO 社区接受

我正在拉取一段代码,去除不必要的代码,然后在我的页面中使用剩余的代码。

该代码包含我不想保留的 link 锚标记,但我需要能够在 link 元素上保留样式。

我目前使用

$tweettext = strip_tags($tweettext, '<div>, <p>, <a>');

哪个有效。但是,给我留下了 link 到损坏的 link 的锚标签(它们被损坏是因为它使用相对 linking 并且是从外部网站提取的)。

如果我用

$tweettext = strip_tags($tweettext, '<div>, <p>');

它删除了不必要的 link,但我现在没有可以应用样式的元素。

我能否在 运行 之前将标签从 'a' 标签交换到 'span' 标签以去除不必要的标签(不需要 'a'一旦 'a's 文本被包裹在 'span')?

所以我可以使用

$tweettext = strip_tags($tweettext, '<div>, <p>, <span>');

我只需要直接交换 'a' 到 'span' 函数。

CODE PON DE REQUEST(与我的实际问题无关,我只是想知道我可以 swap_tags() 或 swap_text() 的函数):

工作代码(利用 preg_match(),我的问题的答案):

<?php
foreach($tweet->find('.tweet-text') as $tweettext) {
    $tweettext = str_ireplace('TweetTextSize TweetTextSize--normal js-tweet-text ', '', $tweettext);
    $tweettext = str_ireplace('data-aria-label-part="0"', '', $tweettext);
    $tweettext = str_ireplace('lang="en" ', '', $tweettext);
    $tweettext = str_ireplace('data-query-source="hashtag_click" ', '', $tweettext);
    $tweettext = str_ireplace(' pretty-link js-nav" dir="ltr" ', '"', $tweettext);
    $tweettext = preg_replace('/href=".*?"/', '', $tweettext);
    $tweettext = str_ireplace('<a', '<span', $tweettext);
    $tweettext = str_ireplace('</a>', '</span>', $tweettext);
    $tweettext = strip_tags($tweettext, '<div>, <p>, <span>');
    if($imgmatches[1] != '') {
        $tweettext = str_ireplace('tweet-text', 'tweet-text tweet-has-bg-text ', $tweettext);
    } else {
        $tweettext = str_ireplace('tweet-text', 'tweet-text', $tweettext);
    }
    echo $tweettext;
}

正确输出:

<p class="tweet-text">
    We’ve got a number of international exhibition stand builds this quarter; including <span class="twitter-atreply" data-mentioned-user-id="441777148">@StocExpo</span> in Rotterdam. This is the 4th year we have undertaken a stand at StocExpo for <span class="twitter-atreply" data-mentioned-user-id="290202396">@Dantecltd</span> <span class="twitter-hashtag">#exhibition</span> <span class="twitter-hashtag">#StocExpo</span>
</p>

谢谢杰森。

没有 "swap_tags" 函数可以解决您的问题,但您可以使用 DOMDocument 来制作您自己的函数,而不是像上面那样替换字符串。下面应该说明如何实现这一点。它将 HTML 字符串加载到 DOMDocument 对象中并搜索所有超链接。找到超链接后,它将通过 DOM 树向后工作以执行 modifications(如果您要向前迭代,它将在第一个 mod 之后停止)

每个遇到的超链接的属性都添加到新创建的 SPAN 元素中 - 您可能希望 mod 确认或添加过滤器以排除某些属性( href实例 )

<?php

    $str='<p class="tweet-text">
        We’ve got a number of international exhibition stand builds this quarter; including 
        <a href="/StocExpo" class="twitter-atreply pretty-link js-nav" dir="ltr" data-mentioned-user-id="441777148">@StocExpo</a>
        in Rotterdam. This is the 4th year we have undertaken a stand at StocExpo for 
        <a href="/Dantecltd" class="twitter-atreply pretty-link js-nav" dir="ltr" data-mentioned-user-id="290202396">@Dantecltd</a> 
        <a href="/hashtag/exhibition?src=hash" data-query-source="hashtag_click" class="twitter-hashtag pretty-link js-nav" dir="ltr">#exhibition</a> 
        <a href="/hashtag/StocExpo?src=hash" data-query-source="hashtag_click" class="twitter-hashtag pretty-link js-nav" dir="ltr">#StocExpo</a>
    </p>';

    libxml_use_internal_errors( true );
    $dom=new DOMDocument;
    $dom->strictErrorChecking=false;
    $dom->validateOnParse=false;
    $dom->recover=true;
    $dom->loadHTML( $str );
    libxml_clear_errors();


    $col = $dom->getElementsByTagName('a');
    if( $col->length > 0 ){

        for( $i=$col->length; $i > 0; $i-- ){
            $node=$col->item( $i );

            if( !empty( $node ) && $node->nodeType==XML_ELEMENT_NODE ){
                $span=$dom->createElement('span', $node->nodeValue );


                foreach( $node->attributes as $attr ){
                    $attribute=$dom->createAttribute( sprintf('data-%s',$attr->nodeName ) );
                    $attribute->nodeValue=$attr->nodeValue;
                    $span->appendChild( $attribute );
                }

                $node->parentNode->replaceChild( $span, $node );
            }
        }


        printf('<textarea cols=100 rows=20>%s</textarea>', $dom->saveHTML() );
    }

?>

Op 不需要 rather a string which is used as the html which makes regex the best operation fitting in this case, the suitable regex expression for the following case turns out, is in this answer
提到的 DOMDocument 对象,这也是

$content = preg_replace("/<a href=.*?>(.*?)<\/a>/","",$content);