views:

25

answers:

1

Hi there,

I've got a string of data, and I want to remove the content between two blocks of text using PHP. Here's an example:

"dataset123"

The text I want is here.

"endfile"

I want everything between those two quoted values. The values won't change, so they can be hard coded.

Any ideas? I've tried searching for something like this. I'm sure I have to use str_match and regex, but I'm not sure how to go about doing it.

Thanks! Jon

+1  A: 
preg_match('!dataset123(.*)endfile!s', $string, $matches);
var_dump($matches[1]);

or

$start = strpos($string, 'dataset123');
$end = strpos($string, 'endfile');
var_dump(substr($string, $start + 10, $end - $start - 10));

ps: due to that limiters are constant literals - i've used constant 10 as string length

zerkms
This is great - I just need some help changing it around a bit. What if I know the starting point, but the end point is just the end of the file, how would I do that? Just remove "endfile" from the match?
Jon
Yup, that worked. Thanks! Now I just need to figure out how to convert this "Wed AUG 4 NATHANIEL RATECLIFF w/ Pearly Gate Music - MEDIA CLUB" into $date, $artist and $venue.
Jon
@Jon: you have to learn how to program, really. We cannot always do your work. Also - if you have another question and had current one answered - check it and create new one.
zerkms
@zerkms Thanks bud. Figured it out.
Jon