silvia zulinka
silvia zulinka

Reputation: 731

How to get numeric before string character in PHP?

I have data for example

8789 WWW xxx 8739

and if the value like this, the value still 786-456 not 786

786-456

I want to get numeric before string character, how to do that?

the result should be

8789

786-456

here is the code that I have made

$string = '123 home/cat1/subcat2/';
$first = strtok($string, "/^[a-zA-Z]");
echo $first;

the result that I want should be

123

Upvotes: 0

Views: 101

Answers (1)

Dimi
Dimi

Reputation: 1277

You can simply cast it into an int, which will convert as much as it can before encountering a non-int character.

$str="8789 WWW xxx 8739";
echo (int)$str;//outputs 8789

Upvotes: 4

Related Questions