You can use
preg_match('~^[a-z]{2}39\.[a-z]{4} \(([\d.]+)~m', $myfile_contents, $matches);
print_r($matches[1]);
See the regex demo
I added a \(
to match a literal (
and used a capturing group around [\d.]+
(1 or more digits or dots) that matches the IP that can be retrieved using [1]
index in $matches
. The ~m
enables a multiline mode so that ^
could match the beginning of a line, not just the string start.
UPDATE
If you need to create a dynamic regex with a literal string in it, you should think about using preg_quote
:
$myfile = file('file.txt');
$search = "pc39.home"; // <= a literal string, no pattern
foreach ($myfile as $lineContent)
{
$lines=preg_match('/' . preg_quote($search, "/") . ' \(([\d.]+)\)/', $lineContent, $out);
echo($out[1]);
}
You also need a single quoted literal in ' \(([\d.]+)\)/'
since the \
must be literal \
symbols (yes, PHP resolves \
as a literal \
with non-existing escape sequences, but why put it to PHP?).