Paul Brennan
Paul Brennan

Reputation: 63

PHP - Searching strings from a certain format

I've seen other examples using regex however I'm a little bit troubled trying to format mine correctly, I'll have a list of numbers like this:

1.0.0.1ACS
1.0.0.2ADS
1.0.1.8AAB

However I only want to have the actual 4 numbers but the numbers could turn into multiple digits per line for example 122.222.222.222 (up to 3) how would I go about using PHP to do this? I presume I would have to put them all into a array first then for each in array, then I am confused on how to remove the extra letters.

Thanks in advance!

Upvotes: 1

Views: 43

Answers (1)

Nikita PenGenKiddy
Nikita PenGenKiddy

Reputation: 47

Yours Code is:

<?php
$input = <<<TEST
1.0.0.1ACS
1.0.0.2ADS
1.0.1.8AAB
TEST;
if (!preg_match_all("#(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})+#", $input, $matches))
    print("NOT FOUND!");
else 
    var_dump($matches[0]);

Upvotes: 2

Related Questions