• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Split String and Number in PHP

#4
Best way is by using a regular expression.

Example:
PHP Code:
$str 'Johnson 54321';

if ( 
preg_match '/\D+([0-9]+)/'$str$matches ) )
{
 
   print_r($matches);

The pattern \D+([0-9]+) says: check for 1 or more non-digit characters first, followed by 1 or more digit characters.
The part that is between braces, will be returned in the $mathes array.

The result will be in $matches[1], which will be equal to '54321'.
Reply


Messages In This Thread
Split String and Number in PHP - by CharlesK - 03-02-2018, 02:42 AM
RE: Split String and Number in PHP - by PaulD - 03-02-2018, 04:31 AM
RE: Split String and Number in PHP - by CharlesK - 03-02-2018, 07:26 AM
RE: Split String and Number in PHP - by Wouter60 - 03-02-2018, 09:52 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.