duancanjiu3754 2019-03-25 16:33
浏览 35
已采纳

使用PHP交换<a> for <span>

Please can someone help me edit my question so it complies with SO rules? I have asked a valid question and received the answer from a helpful SO'er yet it hasn't been well received by the SO community

I am pulling a block of code through, stripping out the unnecessary code then using the remaining code in my page.

The code contains anchor tags who's links I do not wish to keep but I need to be able to leave styling on the link elements.

I currently use

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

Which works. But, leaves me with anchor tags that link to broken links (they are broken as it uses relative linking and is pulled from an external website).

If I use

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

It removes the unneccessary links but I now don't have an element I can apply styles to.

Am I able to swap the tag from an 'a' tag to a 'span' tag before running it through to strip unnecessary tags ('a' isn't needed once the 'a's text is wrapped in the 'span')?

So I can use

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

I just need a straight swap 'a' to 'span' function.

CODE PON DE REQUEST (not that relevant to my actual question, I simply wish to know the function where I can swap_tags() or swap_text()):

Working Code (making use of the preg_match(), the answer to my question):

<?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;
}

Correct Output:

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

Thanks, Jason.

  • 写回答

2条回答 默认 最新

  • dony39517 2019-03-25 17:26
    关注

    Op doesn't need a DOMDocument object as mentioned by RamRaider 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
    Which is also

    $content = preg_replace("/<a href=.*?>(.*?)<\/a>/","",$content);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭