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!
You could use preg_match_all, but I won't be of any help as I am not experienced with regex much. preg_match_all('pattern', 'contents', $array); The third, array, will place the matches in an array that you could loop over to put into a file or insert into a database. I'm sure someone here could help with the pattern. I would try, but again not much experience and understanding in it. To get the contents, you could try file_get_contents or reading each like of the file. See the following... php.net/preg_match_all php.net/file_get_contents http://us.php.net/manual/en/ref.filesystem.php
hi just wrote a quick script for you that will do 99% for you.. rest you can modify that. $fineName="SOME_FILE.txt"; $startSymbol="*start*"; $endSymbol="*stop*"; $allFile=file($fineName); // read complet file in an array and loop through all Lines for($i=0;$i<count($allFile);$i++) { $line=$allFile[$i]; // read File Line By Line $StartPos=-1; $endPos=-1; if(strpos($line,$startSymbol)!==FALSE) // locate the start Sybmol in Line { while(strpos($line,$startSymbol)!==FALSE) // keep looking in same line till start symbol is not found { $StartPos=strpos($line,$startSymbol); // get the position of stgart symbol if(strpos($line,$endSymbol)!==FALSE) // get the position of end symbol { $endPos=strpos($line,$endSymbol); } if($endPos==-1) { // Limitation .. if end symbol is not in same line script will stop working. you can improve it .. but i am in hurry :) echo "END MARKER was Not Found in Same Line. "; exit; } $valueStartPos=$StartPos+strlen($startSymbol); // get the actual start point of your value $valueEndPos=$endPos-$valueStartPos; // get the end point value $value=substr($line,$valueStartPos,$valueEndPos); // obtain the value echo $value ."<br>"; // print $line=substr($line,$endPos+strlen($endSymbol)); // truncate the line and start the line from end position as that loop may not look from start again. } } } PHP: