I think I am missing something obvious here but I can't see what.
I am pulling information from another file using
file(); which works as expected - putting each line in to an array. The file contains some HTML tags but is just a text file (not a full web page). After pulling the information, the array looks like this:
array ( 0 => '<h1>' 1 => 'Header text' 2 => '</h1>' 3 => '<p>' 4 => 'Lots of text to make up a paragraph' 5 => '</p>' 6 => '<p>' 7 => 'More text to make up a paragraph' 8 => '</p>' )
I know that
array_search(); will only return the first key which matches the search. In this case, I want to find the first entry of
<p> so my search looks like this:
array_search("<p>",$file); //$file is the variable containing the above array.
I would expect this to return a value of 3 as that is the first entry in the array which contains
<p> however it is returning nothing. I also tried searching for "Header text" (making sure that the case matches) but this also returns nothing. Using strict doesn't help either.
Contents of the pulled file (we'll just call it "file.txt"):
<h1> Header Text </h1> <p> Lots of text to make up a paragraph </p> <p> More text to make up a paragraph </p>
The PHP file trying to get the key:
<?php $file = file("file.txt"); //Pull the file in to an array. print_r($file); //Display the array to screen to confirm it's being pulled correctly. $key = array_search("<p>",$file); //Search the array for <p> and return the key. echo "\n\n".$key; //Display the key to screen. This is coming up empty. ?>
Just a note: When displaying the array to the browser, due to the HTML tags, some entries show blank but all entries are as expected when you view the source of the page, showing the array is populated as expected.