Getting Mulitple Strings From A File

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
waynobweno
Forum Newbie
Posts: 2
Joined: Fri May 15, 2009 1:34 pm

Getting Mulitple Strings From A File

Post by waynobweno »

Hi there, I am new to php, making a switch over from ColdFusion <YUK! To CF!>

Anywho, I have a large text file that has multiple values in it that I need to extract. All of the values are denoted by *start* the_value *stop* ...

So in my text file, there are thousands of line of text and peppered throughout the text are *start* the_value *stop* values, like so:

Here is a mock up of what I have (this is just fake text to demonstrate):
Augue qui feugait sit ex veniam erat ullamcorper nostrud vero consequat duis eu, ea, consequat at, vulputate ullamcorper wisi elit duis iusto. In luptatum qui vel vel nulla at te eum, accumsan molestie. Esse, et vulputate lorem *start* value_1 *stop* blandit et duis luptatum dolor in, facilisis ut ipsum. Ex *start* value_2 *stop* odio tincidunt ea iusto molestie amet delenit odio ex nisl vulputate adipiscing et. Aliquip, vel *start* value_3 *stop* quis in, dolore feugait nostrud dolore dolor vel praesent duis. Autem nonummy nonummy minim exerci in ullamcorper molestie feugiat ut ullamcorper *start* value_4 *stop* aliquip vulputate. Nulla esse eum duis, tincidunt molestie, nostrud qui, velit, ut illum, vero minim ut consequat, consequat aliquam illum ad ut illum illum suscipit zzril.

Here is the catch, the values for value_1 - 4 are never the same so I cant use a loop to pull them out. Is there someway to just look for *start* and *stop* and grab the text in between. I have managed to do this in CF, I just hate how slow and worthless CF is thus the switch to PHP. Oh yeah, I may have more than 4 values and up to 1000 at a time.

Please help if you can. It would be MUCH appreciated.
Thanks so much!
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: Getting Mulitple Strings From A File

Post by crazycoders »

If i understand right, you are looking for a constant pattern with variable data in between? what you could do is read the whole file in memory if not too large.

Then, do a STRPOS to get the first occurence of *start*, use STRPOS again to find *end* and extract that portion using SUBSTR.

Then, remove everything from start to STRPOS(*end*) using substr such as $content = substr($content, $endpos+strlen($endpostag)) and that will give you one value extracted, extract them continuously until you found all of them and process them the way you need.

You explain vaguely something about values not being sorted, don't worry, instead extract them THEN sort them in memory or else you could loop your string several thousands of times which will bust your cycles and memory badly...
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Getting Mulitple Strings From A File

Post by Darhazer »

Not necessary to read the whole file...
you can read line by line, and search for the *srart* / *stop* markers and buffer them.

If the file do not support embeded markers (*start* *start* value_1 *stop* value_2 *stop*) it is pretty easy task. Use state variable to know if you are in a buffer or not, and search for one of the patterns... use substr and strpos as crazycoders suggested.
Post Reply