0

I could use some help simplifying the following code that includes a regex statement. I am simply trying to get the integers from all possible substrings contained in a larger text and then push those integers onto an array. I know I should not have to use split, but I forget how to do do it with regex.

    preg_match_all('/bar:foo\(?[0-9]*\);/', $in , $tag);
    for ($i = 0; $i < count($tag[0]); $i++) {
        $parts = explode("(", $tag[0][$i]);           
        $parts2 = explode(")", $parts[1]);
        array_push($myArr, $parts2[0]);
    }

2 Answers 2

3

Actually, you might not need to use preg_split, but you can already improve it by modifying your current pattern:

$numbers = preg_match_all('/bar:foo\(?([0-9]*)\);/', $in , $matches) 
           ? array_map('intval', $matches[1])
           : array();
Sign up to request clarification or add additional context in comments.

Comments

1
preg_match_all('/bar:foo\(?([0-9]*)\);/', $in , $tags);
print_r($tags);

Comments

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.