Get index of first dot with letter in a string

Tags: ,



I have a string like this one:

dsl-34.345.324-24718.pool.vodafone9.com (not a real hostname, just an example)

I’ve already tinkered around with preg_split which didn’t gave me the good result.

Anyway, I’d like to get this result:

pool.vodafone9.com

EDIT

Sorry, for not showing directly what I did with preg_split(), I already thought about doing that, however, it would have resulted into a completely wrong result when there are numbers within the domain.

preg_split( "/[0-9].[a-z]/", $hostAddr)

put out

ools.vodafone.com

but with vodafone9.com it would have resulted in

om

Answer

Try using this regular expression to detect everything after the first . followed by a single letter:

$hostname = 'dsl-34.345.324-24718.pool.vodafone9.com';
$matches = array();
preg_match('/(?<=.)[a-zA-Z].+/', $hostname, $matches);
echo $matches[0];


Source: stackoverflow