Dear All,
i have a txt file like:
--start--
jeff
--end--
--start--
sophya
--end--
Now i need to collect the names like jeff and sophya into an array.How i do it?I use below code but its not give me proper output.
$data = file_get_contents('http://localhost/sample.txt');
$start_position = strstr($data,'--start--');
$end_position = strpos($data,'--end--') ;
$value=substr($start_position,$end_position);
print_r($value);
exit();
My out put is : --end-- --start-- sophya --end--
is their any kind heart who help me to take two name into an array?
thanks in advance.riad
There are a few approaches you can use here… I would personally use a regular expression to match what you need.
preg_match_all().Note: This includes the end of line characters on each side. To exclude them from the names, and and whitespace use the pattern
'/--start--\n\s*(.*?)\s*\n--end--/ms'I should mention, the advantage of this is it will automatically ignore things that are not in the correct format.
e.g. In the following file,
asdwill be ignored.