You are here: Re: String parsing question... « PHP Programming Language « IT news, forums, messages
Re: String parsing question...

Posted by PTM on 07/27/06 04:33

"Benjamin Esham" <bdesham@gmail.com> wrote in message
news:pan.2006.07.27.03.58.44.371018@gmail.com...
> 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
>

Assuming that you are only using <start> and </start> tags, and no other <>
</> tag pairs, in the line you're checking, you could use the strip_tags()
command, eg:

$variable_name=STRIP_TAGS($line_read_from_file[$optional_line_counter]);

to do it.
Your tags don't actually have to be called <start> and </start> for this to
work, ANY tag will be stripped.
Tags you want kept will have to be listed as allowable tags, eg:

$variable_name=STRIP_TAGS($line_read_from_file[$optional_line_counter],
"$allowable_tags", "$allowable_tags" );

xhtml tags wont be allowed unless you use the html tag name
eg <br /> should be <br>
You also need to be sure your tags are properly formatted with both < and >
characters or you could get some strange results.

I use strip_tags() in an xml parser and it reduced my code considerably.


Phil

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация