473,670 Members | 2,527 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1428
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.c om | 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.c om | 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($lin e_read_from_fil e[$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($lin e_read_from_fil e[$optional_line_ counter],
"$allowable_tag s", "$allowable_tag s" );

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.c om | 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$wit h 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('<@cont ent>', $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.c om | 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('<@cont ent>', $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.c om | 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
3619
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" -zug into: ,
10
2625
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 integers. What is the best standard(!) C++ way to accomplish this? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
4
1382
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 problem I'm having is that one of the data values has commas in it so its blowing up on the other fields. How can I remove the commas from my string if they exist? example: Getting this BMW, Used, 325C, $19,252.00, Smith
3
2074
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
1412
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 search/replace on the "&" char with the "amp&;" prior to parsing??
0
8384
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8896
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8590
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8659
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6211
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5683
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2035
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1790
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.