How to remove all strings from a string

0

I have the string:

$textMostraMarcado = '3 3 1 6 8 6 8 <b>1 1 1 </b>2 4 2 7 5 <b>4 4 4 4 </b>9 <b>8 8 8 </b>7'

I would like to get the result:

<b>1 1 1 </b>
<b>4 4 4 4 </b>
<b>8 8 8 </b>

That is, get the values that are between the <b></b> tags.

    
asked by anonymous 06.06.2017 / 20:25

1 answer

3

Just use expressions with the preg_match_all function:

  • The first parameter of the function will be the regular expression: /<b>.*?<\/b>/ ;
  • The second parameter will be the text from which the information will be extracted;
  • The third parameter will be the list of extracted information;
  • For example:

    if (preg_match_all("/<b>.*?<\/b>/", $textMostraMarcado, $matches))
    {
      print_r($matches[0]);
    }
    

    The output will be:

    Array
    (
        [0] => <b>1 1 1 </b>
        [1] => <b>4 4 4 4 </b>
        [2] => <b>8 8 8 </b>
    )
    
      

    See working at Ideone .

        
    06.06.2017 / 20:41