-1

i.e. i have a string, which contains ANY,ANY kind of symbols,words, sentences:

<START>%6tge9ruj+_]`\Qe,3[][}~[wq]we-Oke|\;'"_p}|P{dl3=+fmwfoe-f <END>

how to:

1) get everything between <start> and <end>

2) replace everything <start> and <end>

thanks for answering!

T.Todua
  • 53,146
  • 19
  • 236
  • 237

1 Answers1

1

Extracting the part between tags with regexp:

<?php

$text = '<START>%6tgruj+_]`\\Qe,3][}~[e-Oke|;\'"=+fmwfoe-aaa<END>aaaa<END>';
$pattern = '/<START>(.*?)<END>/';   //<-------- till the first occurence of <END>
$pattern = '/<START>(.*)<END>/';    //<-------- till the last occurence of <END>
$out = array();

preg_match($pattern, $text, $out);      var_dump($out);
?>

Demo. But it's really not a job for regular expressions, you should think about using some parser.

T.Todua
  • 53,146
  • 19
  • 236
  • 237
gronostaj
  • 2,231
  • 2
  • 23
  • 43
  • 1
    If you _were_ to use something like this, and as mentioned above you're probably going to want a document parser instead, it'd be worth saying you'd probably want '.*?' as opposed to '.*', as if it's embedded in a wider document the regex engine's gunna be wasting a lot of time trying to maximise the match. – jstephenson Feb 07 '13 at 18:55
  • 1
    @jstephenson do you have an example of a document parser that parses `xxx` – PeeHaa Feb 07 '13 at 18:58
  • @PeeHaa Whilst that doesn't alter the relevance of my comment in the main, and as delimiters in a stream which turns out not to be a pass at some XML-esque format would seem a peculiar thing to do, so a nudge in the direction of doc parsers is based upon a not-unreasonable widening of the scope. – jstephenson Feb 07 '13 at 19:08
  • Well considering everyone in here is shouting "use a parser" I would really like to at least see some example of such a parser everybody is talking about. – PeeHaa Feb 07 '13 at 19:10