1

I have this kind of string:

1234567890(number without specific number of character)-(minus symbol serves as divider)text

How can I see and extract number before - symbol?

2 Answers 2

4

Use a regex like this:

preg_match('/^(\d+)/', '123-something', $match);

This will capture all decimal digits from the start of the string until the first non-decimal digit, and the result will be in the $match variable.

if (preg_match('/^(\d+)/', $string, $match)) {
    $number = $match[1];
}
Sign up to request clarification or add additional context in comments.

Comments

2

Use preg_split()

$bla=preg_split('#(?<=\d)-(?=[a-z])#i', "1234567890-asdlkj");
echo $bla[0];

gives 1234567890

If you want the text field,you can use

echo $bla[1];

gives asdlkj

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.