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.
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);