0

Consider the string 12345aaa. I want to use preg_split() on it so only the numeric part of it will be returned (i.e. 12345). How would I write the regex for it ?

1 Answer 1

5

preg_split is not the function you want, you want preg_match as you don't want to split a string into parts, but to retrieve part of it. As I don't know more details, I can only provide a rough example of how to do it.

$reg = null;
//match the first set of 1 or more numbers
if ( preg_match('/\d+/', $str, $reg) ){
    $num = $reg[0];
}
Sign up to request clarification or add additional context in comments.

1 Comment

No need to wrap \d+ in parentheses. Use $reg[0]. It contains the full match.

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.