Jump to content

Need To Extract Text From File, Not Sure How


waynobweno

Recommended Posts

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* the_value *stop* blandit et duis luptatum dolor in, facilisis ut ipsum. Ex *start* the_value *stop* odio tincidunt ea iusto molestie amet delenit odio ex nisl vulputate adipiscing et. Aliquip, vel *start* the_value *stop* quis in, dolore feugait nostrud dolore dolor vel praesent duis. Autem nonummy nonummy minim exerci in ullamcorper molestie feugiat ut ullamcorper *start* the_value *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.

 

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!

 

This should get you started...

 

<?php
$data = "textFileHere.txt";

//find the name of the text between the start and stop markers
$text_begin = strPos($data, "*start*");
$text_end = strPos($data, "*stop*", $text_begin);
$text = substr($data, ($text_begin + 7), ($text_end - $text_begin - 7));

//echo text to screen
echo "<b>Here is the text: </b>".$text."<br />\n";
?>

 

That should get you off to a good start.  You may have to mess with the numbers in there a little bit to get the correct formatted string but it should be pretty close.  You will have to create a loop to cycle through and get multiple values.  I'll let you figure that part on your own as I'm honestly just too lazy right now.

Here's an approach that uses regular expressions. May not be what you need, but may help you in your quest.

<?php
$data       = "textFileNameHere.txt";
$contents = file_get_contents($data);
$res         = array();
preg_match_all('/\*start\*(.*?)\*stop\*/',$contents,$res,PREG_PATTERN_ORDER);
$res = $res[1];
print_r($res); // prints an array of matches
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.