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 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'