How to remove all strings from a string

Asked

Viewed 32 times

0

I got 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 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 tags <b></b>.

  • Can you describe in words what that result would be? You want to take the content that is between the tags <b></b>?

  • Exactly that, get the sequences in bold. Thank you

1 answer

3


Just use expressions with the function preg_match_all:

  1. The first parameter of the function will be the regular expression: /<b>.*?<\/b>/;
  2. The second parameter will be the text from which the information will be extracted;
  3. 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 exit will be:

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

See working on Ideone.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.