-4

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.

Jason Is My Name
  • 929
  • 2
  • 14
  • 38

2 Answers2

1

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);
weegee
  • 3,256
  • 2
  • 18
  • 32
  • Thank you very much, I knew it was possible but couldn't for the life of me remember what it was called in PHP - please upvote my question, Thanks! – Jason Is My Name Mar 25 '19 at 17:27
0

There is no "swap_tags" function to solve your problem but you could craft your own using DOMDocument rather than string replacement as above. The following ought to demonstrate how that might be accomplished. It loads the HTML string into the DOMDocument object and searches for all hyperlinks. When it has found the hyperlinks it will work backwards through the DOM tree to perform modifications ( if you were to iterate forwards it would stop after first mod )

The attributes from each encountered hyperlink are added to a newly created SPAN element - you might wish to modify that or add a filter to exclude certain attributes ( href for instance )

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

?>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46