|
Posted by Fred!head on 07/27/06 05:03
Benjamin Esham wrote:
> timslavin wrote:
>
> > First, I want to grab data between two markers that I define (e.g.
> > <start> ... data here ... </start> and strip of the text before the first
> > marker (<start> in this example) and after the second marker (</start>) in
> > this example. That would leave me with the "... data here ..." chunk with
> > my markers either included (worst case) or removed (best case, saving me
> > the third step below).
>
> $pieces = preg_split('/\<(\/)?start\>/', $input);
> $chunk = $pieces[1];
>
> Assuming that $input is your input data, $chunk will contain your "data
> here" segment. What this does is to split the data into an array; the
> regular expression passed to preg_split() matches both the <start> tag and
> the </start> tag, so the array has three elements. The 0th element contains
> everything before <start>, the 1st contains everything between the tags, and
> the 2nd contains everything afterwards. (Note that this is untested; my
> regular expression might be wrong. Looking at [1] should help you with the
> regex syntax expected by preg_split().)
>
> [1] http://perldoc.perl.org/perlre.html
>
> > Is this one PHP function or two functions? I see that strstr will get me
> > everything to the right of <start> but I cannot figure out how to remove
> > everything to the right of </start> so that I only have the data chunk I
> > want (what's between these two markers).
>
> You could probably call strstr() twice and then substr(), but IMO using
> preg_split() is way easier.
>
> > Second, I want to substitute values for values found in the data chunk. I
> > know str_replace does that just fine.
>
> Yep. If you need even more advanced replacing functionality look at
> ereg_replace() and preg_replace().
>
> > Third, I then want to strip out the markers from my data chunk.
>
> This will be done as a side effect of preg_split().
>
> HTH,
> --
> Benjamin D. Esham
> bdesham@gmail.com | AIM: bdesham128 | Jabber: same as e-mail
> "...more and more of our imports are coming from overseas."
> - George W. Bush
Thanks, Benjamin, and for the Bush quote: very obvious and funny.
Probably it's the fact my mind goes blank when reading about regular
expressions but I'm not able to make the preg_split work. If you have
time/interest, I'd appreciate any additional thoughts.
Basically I'm pulling a template from a database field then performing
operations on that data. Within the template I have this data:
.... stuff here ...
<@content limit="" ... more elements ... >
<h2><$Title$></h2>
<$Content$>
</content@>
.... more stuff here ...
So I'm trying to grab everything between the end of <@content and
</content@> as a single data chunk that I can then perform operations
on (like replacing <$Title$> and <$Content$> with result set data from
another query).
What modifications to the preg_split do I need to make this work? Is
there a cleaner way to set up the <content> tags, like </content>
instead of </content@> that would make the regular expression more
efficient? I like using the @ as a flag to find the start marker, on
the premise that makes false results less likely, but maybe I'm
deluded.
I appreciate your help so far! Thank you.
Tim
Navigation:
[Reply to this message]
|