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.
答案 0 :(得分:1)
Op不需要RamRaider所提到的DOMDocument
对象,而是一个字符串,该字符串用作html,使regex在这种情况下成为最佳的操作,下面是合适的regex表达式大小写在this answer
也是
$content = preg_replace("/<a href=.*?>(.*?)<\/a>/","",$content);
答案 1 :(得分: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() );
}
?>