交换 for using PHP

问题描述 投票:-4回答:2

请有人帮我编辑我的问题,以便它符合SO规则吗?我已经提出了一个有效的问题并从一个有用的SO'er那里得到了答案,但它并没有得到SO社区的好评

我正在通过一段代码,删除不必要的代码,然后使用我页面中的剩余代码。

代码包含我不希望保留链接的锚标签,但我需要能够在链接元素上留下样式。

我目前正在使用

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

哪个有效。但是,给我留下链接到断开链接的锚标记(它们在使用相对链接时被破坏,并从外部网站中提取)。

如果我使用

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

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

我可以将标签从'a'标签交换到'span'标签,然后再运行它以去除不必要的标签(一旦'a的文字被包裹在'span'中,就不需要'a')?

所以我可以使用

$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>

谢谢你,杰森。

php strip-tags
2个回答
1
投票

Op不需要像DOMDocument所提到的RamRaider对象,而是一个用作html的字符串,这使得正则表达式成为这种情况下的最佳操作拟合,以下情况的合适的正则表达式,在this answer中 这也是

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

0
投票

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

来自每个遇到的超链接的属性被添加到新创建的SPAN元素 - 您可能希望修改它或添加过滤器以排除某些属性(例如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() );
    }

?>
© www.soinside.com 2019 - 2024. All rights reserved.