1

I have a string like this:

xxx<tag1>ABC</tag1>xxxyyyzzz<tag2>MNO</tag2>zzzz<tag1>EFG</tag1>

and I need transform this into

<tag1>ABC</tag1><tag2>MNO</tag2><tag1>EFG</tag1>

I must extract tag and it's inner content only.

I search regexp what delete all the other content

$string2 = eregi_replace($reg, $string1)
Matej Kormuth
  • 2,139
  • 3
  • 35
  • 52

2 Answers2

2
$str = "<tag1>ABC</tag1>xxxyyyzzz<tag2>MNO</tag2>zzzz<tag1>EFG</tag1>";

preg_match_all('#<tag[0-9]>(.*?)</tag[0-9]>#i',$str,$result);

print_r($result[0]);

will output

Array
(
    [0] => <tag1>ABC</tag1>
    [1] => <tag2>MNO</tag2>
    [2] => <tag1>EFG</tag1>
)
gfcodix
  • 181
  • 5
1

Try this:

$str = "xxx<tag1>ABC</tag1>xxxyyyzzz<tag2>MNO</tag2>zzzz<tag1>EFG</tag1>";
echo preg_replace('/(.*?)(<tag\d+>)(.*?)<\/tag\d+>(.*?)/','$2$3$4', $str);

Note: eregi is deprecated

thecodeparadox
  • 86,271
  • 21
  • 138
  • 164