0

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?

Jason Is My Name
  • 929
  • 2
  • 14
  • 38
  • 1
    Trying to use regex/string replace etc. is prone to errors and *usually* causes problems as it doesn't have any idea of the context of the content. The duplicate should help start something, if you get stuck come back and I'm sure you will get some more help. – Nigel Ren Jul 24 '20 at 16:32
  • @NigelRen I'm already very far through the use of simple-html-dom, this is the thing I require help on :s – Jason Is My Name Jul 24 '20 at 16:34
  • @NigelRen I have added a more direct copy of my code so you can understand where I am up to – Jason Is My Name Jul 24 '20 at 16:36
  • It helps to mention this when creating the question, I've added the tag as well. – Nigel Ren Jul 24 '20 at 16:37
  • @NigelRen I can appreciate why it was closed / already answered. Thanks for reopening! – Jason Is My Name Jul 24 '20 at 16:38

2 Answers2

0

You can do like this

$str = '<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>';

$get_only_span    = str_replace(array("<div>","</div>"),'',$str);
$explode_to_array = explode("<span",$get_only_span);
$filter           = array_filter(array_map('trim', $explode_to_array));

$new_span = '<div>';
foreach($filter as $key){
     if (strpos($key, 'pic.example.com') == false) {
        $new_span .= '<span'.$key;
     }
}
$new_span .= '</div>';

print_r($new_span);


/*
 <div>
  <span class="all-the-same-classes">example text</span> 
  <span class="all-the-same-classes">example text</span>
 </div>
*/

Online Test

Younes Zaidi
  • 1,180
  • 1
  • 8
  • 25
0

I have managed to achieve what I wanted using this format:

foreach($article->find('.article-text') as $articletext) :
    foreach($articletext->find('a') as $articletextanchors) :
        if (strpos($articletextanchors, 'pic.twitter.com') !== false) :
            $removethislink = $articletextanchors;
        endif;
    endforeach;
    $articletext = str_ireplace($removethislink, '', $articletext);
endforeach;

Where the code is iterating through the links present in the article text, looking for the string, creating a variable equal to the full anchor, then removing it from the articles text.

halfer
  • 19,824
  • 17
  • 99
  • 186
Jason Is My Name
  • 929
  • 2
  • 14
  • 38