1

I have been trying to extract specific words from an array which was created from a text file using file() function in php.

The text file sample.txt is like this:

A registration has been received. 

Name: test
Lastname: test2 
Email: [email protected]
Company/School: Test2
Company2: test3
Address1: test4
Address2: test5
City: test6
State: test7
Country: test8
Zipcode: test9

Now I have used file() function to put this text file into an array.

$file_name='sample.txt';
$file_array=file($file_name);

Then I traversed through loop to extract each value and search for word say 'Name' from this array.

    $data1='Name';

    foreach($file_array as $value){
    if(stripos($value,$data1)===FALSE)
        echo "Not Found";
     else 
       echo "Found";
    }

But it always prints 'Not Found'. I have tried using strpos,strstr, preg_match but to no avail. Also if I use a normal array of words instead of creating from file, it works properly.

Thanks in Advance.

Update: My objective in this problem is to first detect what field it is ex. 'Name' and then its value ex. 'test'

2 Answers 2

1

It certainly could be a line ending problem or an issue with your file encoding, I'm not sure exactly how file() handles whitespace either.

As a suggestion on how to improve the code, if you create yourself a keyed array from the data then you can use the more reliable array_key_exists() function to search for your fields.

$file_name = 'sample.txt';
$file_data = file($file_name);

// loop through each line
foreach ($file_data as $line) {

// from each line, create a two item array
$line_items = explode(":", $line);

// build an associative (keyed) array
// stripping whitespace and using lowercase for keys
$file_array[trim(strtolower($line_items[0]))] = trim($line_items[1]);
}

Now you can use array_key_exists as follows:

if (array_key_exists("name", $file_array) === false) {
  print "Not found.";
} else {
  print "Found.";  
  // and now it's simple to get that value
  print "<br />Value of name:  " . $file_array['name'];
}
Sign up to request clarification or add additional context in comments.

4 Comments

I would try using a print_r($file_array) and determining what your keys look like.
I see your point. So the keys are actually numbers like this "[2] => Name: test". So I can get the Name from specifying a number and in this case it is "2" instead of "name". But in this case I have to see the array key first and then change the value accordingly so if position of "Name" is changed in any of the text file, my script would break. But Thanks a lot anyways as this can work for now.
Also here I am not exactly searching for string "Name" but just looking at position and extracting value of it without able to check whether it is contains "Name" field or not.
Well, what I was trying to hint you toward was a situation where that array looked like this: name => test, lastname => test2, email => [email protected], etc. In this way, each string to left of your colon becomes a key in the array, and thus, searchable with array_key_exists().
0

most likely you still have newline characters at the end of each "line" in your array. Try loading it like this:

$file_array=file($file_name, FILE_IGNORE_NEW_LINES);

5 Comments

Basically this--> array(13) { [0]=> string(70) "ÿþA registration has been received. " [1]=> string(1) "" [2]=> string(21) "Name: test" [3]=> string(33) "Lastname: test2 " .....contd.}
string(21) for "Name: test"? Is the input file in UTF-16? You'd need to conver to UTF-8 or use the mb_stripos (multi-byte aware) string fucntions.
Tried mb_stripos but again didnt work. Actually I also tried converting each value to utf-8 before with $value=utf8_encode($value) and it didnt work either.
I tried mb_stripos with internal encoding like mb_stripos($value,$data1) and also specially mentioning utf-8 like mb_stripos($value,$data1,"UTF-8"). Is it correct ?
Yeah, but utf8_encode() is for iso8859-1 -> utf8, You're starting with utf-16. Try mb_convert_encoding() instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.