473,386 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

String parsing question...

Hi,

I'm trying to do something with PHP and I'm not 100% familiar with PHP
as I am with VBScript. So if you could bear with me on what is likely a
stupid question, I'd appreciate it!

I have a chunk of text with a variety of tags inside the text. I want
to perform the following process to this chunk of text:

First, I want to grab data between two markers that I define (e.g.
<start... data here ... </startand strip of the text before the
first marker (<startin 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).

Is this one PHP function or two functions? I see that strstr will get
me everything to the right of <startbut I cannot figure out how to
remove everything to the right of </startso that I only have the data
chunk I want (what's between these two markers).

Second, I want to substitute values for values found in the data chunk.
I know str_replace does that just fine.

Third, I then want to strip out the markers from my data chunk. The
<startmarker has elements to it (e.g. limit=) so I'd need something
that would grab everything from <start to the close of the bracket
(e.g. remove <start limit=1>) to remove it from my data chunk. And
finally I would then want to remove the </startmarker from my data
chunk.

Is this do-able in PHP with a couple functions? Or does it require lots
of string manipulation and lots of functions? Or is it impossible?

Thanks in advance for any insight or pointers to PHP string functions
that'll help!

Tim

Jul 27 '06 #1
5 1415
timslavin wrote:
First, I want to grab data between two markers that I define (e.g.
<start... data here ... </startand strip of the text before the first
marker (<startin 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 <starttag and
the </starttag, 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 <startbut I cannot figure out how to remove
everything to the right of </startso 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
bd*****@gmail.com | AIM: bdesham128 | Jabber: same as e-mail
"...more and more of our imports are coming from overseas."
— George W. Bush

Jul 27 '06 #2
PTM
"Benjamin Esham" <bd*****@gmail.comwrote in message
news:pa****************************@gmail.com...
timslavin wrote:
>First, I want to grab data between two markers that I define (e.g.
<start... data here ... </startand strip of the text before the first
marker (<startin 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 <starttag and
the </starttag, 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 <startbut I cannot figure out how to remove
everything to the right of </startso 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
bd*****@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 <startand </starttags, 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 <startand </startfor 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
Jul 27 '06 #3
Benjamin Esham wrote:
timslavin wrote:
First, I want to grab data between two markers that I define (e.g.
<start... data here ... </startand strip of the text before the first
marker (<startin 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 <starttag and
the </starttag, 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 <startbut I cannot figure out how to remove
everything to the right of </startso 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
bd*****@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 <contenttags, 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

Jul 27 '06 #4
Fred!head wrote:
Benjamin Esham wrote:
timslavin wrote:
First, I want to grab data between two markers that I define (e.g.
<start... data here ... </startand strip of the text before the
first marker (<startin 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);

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.
Whoops, I completely forgot that your opening tag has attributes! Sorry
about that. Try this:

$pieces = preg_split('/\<(@content[^>]*|\/content@)\>/', $input);
What modifications to the preg_split do I need to make this work? Is there
a cleaner way to set up the <contenttags, like </contentinstead of
</content@that would make the regular expression more efficient?
Actually, if you used, for example, <@contentfor both the start and the
end, you could simply do

$pieces = explode('<@content>', $input);

and bypass regular extensions altogether. The resulting array will be set
up the same as before. If you are able to modify the input to make both
tags the same, this would probably be the best solution.

HTH,
--
Benjamin D. Esham
bd*****@gmail.com | AIM: bdesham128 | Jabber: same as e-mail
....and that's why I'm not wearing any pants.

Jul 27 '06 #5
Thanks!

Tim
Benjamin Esham wrote:
Fred!head wrote:
Benjamin Esham wrote:
timslavin wrote:
>
First, I want to grab data between two markers that I define (e.g.
<start... data here ... </startand strip of the text before the
first marker (<startin 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);
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.

Whoops, I completely forgot that your opening tag has attributes! Sorry
about that. Try this:

$pieces = preg_split('/\<(@content[^>]*|\/content@)\>/', $input);
What modifications to the preg_split do I need to make this work? Is there
a cleaner way to set up the <contenttags, like </contentinstead of
</content@that would make the regular expression more efficient?

Actually, if you used, for example, <@contentfor both the start and the
end, you could simply do

$pieces = explode('<@content>', $input);

and bypass regular extensions altogether. The resulting array will be set
up the same as before. If you are able to modify the input to make both
tags the same, this would probably be the best solution.

HTH,
--
Benjamin D. Esham
bd*****@gmail.com | AIM: bdesham128 | Jabber: same as e-mail
...and that's why I'm not wearing any pants.
Jul 27 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Freddie | last post by:
Happy new year! Since I have run out of alcohol, I'll ask a question that I haven't really worked out an answer for yet. Is there an elegant way to turn something like: > moo cow "farmer john"...
10
by: Christopher Benson-Manica | last post by:
(if this is a FAQ, I apologize for not finding it) I have a C-style string that I'd like to cleanly separate into tokens (based on the '.' character) and then convert those tokens to unsigned...
4
by: igotyourdotnet | last post by:
I have a question. I'm reading a CSV file that is uploading to my SQL db, I'm parsing out the file line by line. I'm getting the values and putting them into an arrayList seperate by commas. The...
3
by: dimasteg | last post by:
Hi all C. Nead some help with string "on the fly" parsing, how it can be realized ? Any ideas? I got some of my own, but it's interesting to get other points of view . Regards.
0
by: bruce | last post by:
Hi Fredrick Thanks for the reply. But since I don't have control of the initial text, is there something with python that will strip/replace this... or are you saying I should do a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.