How to find word or number with $search_string

39 views Asked by At

For a function $search_string I want to extract an exact word (here number) from a file. If for example in my text file I have these lines:

file1: 2 file2: 23 file3: 32

then with the command $search_string = "file1"; I get result of file1 be "2" while I want that if I make a $search_string = "file2"; it takes me the result of file2 be "23".

How can I modify this code or other piece of code to extract an exact word or number from a text file?

file.txt :

file1: 2
file2: 23
file3: 32
<?php 
$lines_array = file("file.txt");
$search_string = "file2";
foreach($lines_array as $line)
{
if(strpos($line, $search_string) !== false)
{
list(, $new_str) = explode(":", $line);
}
}
echo $new_str;
?> 
0

There are 0 answers