$content = file_get_content("/path/to/myfile.txt", "r");
if (false === $content) {
// handle error if file can't be open or find
}
preg_match_all('/etc="(.*?)"/', $content, $matches);
echo implode($matches[1], ',');
With file_get_content
you retrieve what's int he file.
After that you need to check if file_get_content
has returned an error code (false
in this case).
preg_match_all
will use RegExp to filter out only what you need. In particular:
/ #is a delimiter needed
etc=" #will match literally the letters etc="
(.*?) #is a capturing group needed to collect all the values inside the "" part of etc value. So, capturing group is done with (). .* will match every character and ? make the quantifier "non greedy".
/ #is the ending delimiter
All matches are collected inside $matches
array (is not necessary that $matches
is previously defined.
Finally, you need to transform the collected values into a string and you can do this with implode
function.