If I have a block of code that contains multiple spans like this:
<div>
<span class="all-the-same-classes">example text</span>
<span class="all-the-same-classes">example text</span>
<span class="all-the-same-classes">pic.example.com/A1B2C3</span>
</div>
This code is in string format under a variable $codeblock.
I wish to remove / delete the last span element using PHP.
There are other spans with the same classes, which I want to keep. I also cannot just target the last span, it must be done using its innerhtml. The innerhtml must contain pic.example.com & will be followed by a random string of alphanumeric characters.
I have the whole block of code being read as a string where I can apply preg_replace & str_ireplace.
Can anyone help me target this span by its innerhtml & remove it? I have been using str_ireplace, strip_tags & preg_replace but cannot figure it out on this particular way of targeting the innerhtml.
I would like to provide you with an example, but I don't know where to start. I imagine it will be:
// block of code as string - run a replace on it
$codeblock = preg_replace(
// if in the block of code there is a span that contains pic.example.com
if(strpos($codeblock->span->innertext, 'pic.example.com')),
// replace with blank
'',
// return new code block as itself
$codeblock);
As you can see I am nowhere near, how do I combine all these PHP functionalities into what I require?
EDIT adding a full block of code so there is more context:
foreach($article->find('.article-text') as $articletext) {
$articletext = str_ireplace('articleTextSize articleTextSize--normal js-article-text ', '', $articletext);
$articletext = str_ireplace('data-aria-label-part="0"', '', $articletext);
$articletext = str_ireplace('lang="en" ', '', $articletext);
$articletext = str_ireplace('data-query-source="hashtag_click" ', '', $articletext);
$articletext = str_ireplace(' pretty-link js-nav" dir="ltr" ', '"', $articletext);
$articletext = preg_replace('/href=".*?"/', '', $articletext);
$articletext = str_ireplace('<a', '<span', $articletext);
$articletext = str_ireplace('</a>', '</span>', $articletext);
$articletext = strip_tags($articletext, '<div>, <p>, <span>');
$articletext = preg_replace(if(strpos($articletext->span->innertext, 'pic.example.com')),'', $articletext);
if ($articlehasimageshowing == 0) {
$articletext = str_ireplace('article-text', 'article-text article-has-bg-text ', $articletext);
} else {
$articletext = str_ireplace('article-text', 'article-text', $articletext);
}
echo $articletext;
}
EDIT I have tried my best and this is as far as I have got (it breaks though but I think it is close):
foreach($article->find('.article-text') as $articletext) :
$articletextacounter = 1;
foreach($articletext->find('a') as $articlea) : ?>
<!-- New A found! -->
<?php
echo $articlea;
$articleatext = $articlea->textContent;
echo $articleatext;
if (strpos($articleatext, 'pic.twitter.com') !== false) :
unset($articlea[$articletextacounter]);
endif;
$articletextacounter++;
endforeach;
$articletext = str_ireplace('articleTextSize articleTextSize--normal js-article-text ', '', $articletext);
$articletext = str_ireplace('data-aria-label-part="0"', '', $articletext);
$articletext = str_ireplace('lang="en" ', '', $articletext);
$articletext = str_ireplace('data-query-source="hashtag_click" ', '', $articletext);
$articletext = str_ireplace(' pretty-link js-nav" dir="ltr" ', '"', $articletext);
$articletext = preg_replace('/href=".*?"/', '', $articletext);
$articletext = str_ireplace('<a', '<span', $articletext);
$articletext = str_ireplace('</a>', '</span>', $articletext);
$articletext = strip_tags($articletext, '<div>, <p>, <span>');
if ($articlehasimageshowing == 0) {
$articletext = str_ireplace('article-text', 'article-text article-has-bg-text ', $articletext);
} else {
$articletext = str_ireplace('article-text', 'article-text', $articletext);
}
echo $articletext;
endforeach;
Where might this edit be going wrong?