I'm trying to remove a specific 'span' call without affecting other spans.

The code below returns an error - how can I achieve this?

$exampleString = '<span class="caps">THIS</span> <span class="caps">IS</span> <span class="caps">IN</span> <span class="caps">CAPS</span>';

$fixed = eregi_replace('^(<span class="caps">)([a-zA-Z0-9[:space:]\s[:punct:])+(</span>)','',$exampleString);

Error Message:
<b>Warning</b>: eregi_replace() [<a href='function.eregi-replace'>function.eregi-replace</a>]: REG_EBRACK in <b>PHPDocument3</b> on line <b>4</b><br />

I know this is a newbie question, and I will continue researching all the ins and outs of regex, but I need to get this fixed quick (this is happening on the production server) so I'm posting here - I apologize for wasting anyone's time and thank you in advance to anyone willing to assist me.

    I would do the following (assuming I correctly understand the requirement):

    $fixed = preg_replace('#^<span class="caps">.*</span>#iU', '', $exampleString);
    

    After the second "#" delimiter, the "i" means case-insensitive and the "U" modifier toggle the matching to "ungreedy" so that only the smallest amount of text that matches will be used (e.g.: only one <span>...</span> section).

      Wouldn't this in turn remove everything in the string between the first <span class="caps"> and the last </span>?

      I'd assume I'd somehow have to do something like this (pseud.. obviously)

      $fixed = preg_replace('#^<span class="caps">.*[and NOT </span>]</span>#iU', '', $exampleString); 
      
      Am I correct in this assumption?
      

        Yeah - what you gave returned this:

        <span class="caps">IS</span> <span class="caps">IN</span> <span class="caps">CAPS</span>

        I tried this:

        <?
        $exampleString = '<span class="caps">THIS</span> <span class="caps">IS</span> <span class="caps">IN</span> <span class="caps">CAPS</span>';
        
        $fixed = preg_replace('#^<span class="caps">#iU', '', $exampleString); 
        $fixed = preg_replace('#^</span>#iU', '', $fixed); 
        
        echo $fixed;
        ?>
        

        but it only removed the first <span class="caps"> from the string - nothing else

        :-/

          Got it:

          $fixed = preg_replace('#<span class="caps">*#', '', $exampleString); 
          $fixed = preg_replace('#</span>*#', '', $fixed); 
          

          Thanks for your help - you definately pointed me in the right direction.

            I misunderstood what you wanted. If you want to get rid of all span tags of class "caps":

            $fixed = preg_replace('#<span class="caps">(.*)</span>#iU', "$1", $exampleString);
            

              Yeah but I wanted to keep the content within the span tags

                lazzerous wrote:

                Yeah but I wanted to keep the content within the span tags

                Which is what my last example should do.

                  lazzerous wrote:

                  Got it:

                  $fixed = preg_replace('#<span class="caps">*#', '', $exampleString); 
                  $fixed = preg_replace('#</span>*#', '', $fixed); 
                  

                  Thanks for your help - you definately pointed me in the right direction.

                  Well I think your second preg_replace will violate your initial requirement,

                  I'm trying to remove a specific 'span' call without affecting other spans.

                  because this will remove all the end tags of all the span tags.

                    Hope this would help,

                    $exampleString = '<span class="dontremove">test</span><span class="dontremove">test1</span><span class="caps">THIS</span><span class="dontremove">niroshan</span> <span class="caps">IS</span> <span class="dontremove">test</span><span class="caps">IN</span> <span class="caps">CAPS</span>';
                    
                    $fixed = preg_replace('#<span class="caps">(.+?)</span>#is', '', $exampleString);
                    
                    echo $fixed;
                    
                      niroshan wrote:

                      Hope this would help,

                      Which is substantially equivalent to NogDog's solution (except that his would work if the span happened to be empty and yours would work if the content of the element was spread over more than one line).

                        Write a Reply...