1

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!

2
  • based on your example, what is the expected result you're looking for? Commented Nov 23, 2016 at 16:23
  • Perhaps another array which will output 1.0.0.1 only, rather than 1.0.0.1ACS Commented Nov 23, 2016 at 16:35

1 Answer 1

2

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]);
Sign up to request clarification or add additional context in comments.

3 Comments

Welcome to Stack Overflow! While this code snippet may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post. Remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.
Thank you! Perfect!
Paul, if u place script on older php, u can get error. If u get error, try change from , $matches to , &$matches

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.