I Have a text file and want to parse it with PHP
any one has any idea how can I get IMPATTO LTD
IMPATTO LTD ZAHLUNG: A
from the line above ?
I was thinking maybe regex but there is no uniform way of extracting it.. I guess the best is an expression that will get me the text until the Z from Zahlung.. Is this possible ?
By the way the first words can be of variable length...
$string = null;
$itms = explode('ZAHLUNG', $line);
if(is_array($itms) AND isset($itms[0])) {
$string = trim($itms[0]);
}
echo $string; // IMPATTO LTD
You were on the right track. preg_match is your friend for this.
$pattern = "/IMPATTO LTD/";
$matched = preg_match($pattern, $string);
Read up on the man page as this function is very powerful and has several useful parameters I did not use in the simple example I posted.
UPDATE:
Pardon my bad english it seems I completely misunderstood the question, will delete this post if it does not address proper issue.
try this one, it reads the file into an array and then loops througth the array line by line to see if your text is found
$lines = file('test.txt');
$string_to_find = 'IMPATTO LTD';
foreach ($lines as $line) {
if(strstr($line,$string_to_find)){
echo 'found'. $string_to_find;
}
}
If ZAHLUNG:
is a fixed separator:
<?php
$string = 'IMPATTO LTD ZAHLUNG: A';
if( preg_match('/^(.+)ZAHLUNG:/', $string, $matches) ){
var_dump( trim($matches[1]) );
}
$pos = strpos($line, 'ZAHLUNG');
$string = trim(substr_replace($line, '', $pos));
if 'ZAHLUNG' is constant then it's a bad idea to use regex.