How to split numbers from a string using php

Do you know How to split numbers from a string using PHP? maybe you don’t, that’s why you are here right?. I will show you that

Here we have a string

My customer id is 12457 and phone number is 8147588715

This is our string we have to assign this to a variable.

$str = "My customer id is 12457 and phone number is 8147588715";

Now we have to use explode()  Function to split this string.

$split = explode(" ",$str);

then we are looping all the words and find the numbers

foreach($split as $words)
   {
      echo $words;
   }

Now we taking the INT values from the $words

$numberinthisstring =  (int) filter_var($words, FILTER_SANITIZE_NUMBER_INT);

Now we check if the has any digits, display that



if($numberinthisstring!=0)
       {
           echo $numberinthisstring.'<br>';
       }

 

Now i will give you the complete source code,

$str = "My customer id is 12457 and phone number is 8147588715";
$split = explode(" ",$str);
foreach($split as $words)
{
    $numberinthisstring =  (int) filter_var($words, FILTER_SANITIZE_NUMBER_INT);
    if($numberinthisstring!=0)
    {
        echo $numberinthisstring.'<br>';
    }
}

And this will be the result

So you can list out all the numbers in the string . Hope this helpful for you .

 

Post Your Questions on our forum

Post a question on Forum

Ajith Jojo Joseph

Self taught, dedicated young entrepreneur with many licensed products under his sleeve. Passionate about technology, business and excellence in general.

Share with your friends:

Comments are closed.

How to integrate Paypal API in Laravel

Are you looking to integrate Paypal API in your Laravel project for seamless payment processing? Look no further! In this […]

April 3, 2024

How to integrate Razorpay API in Laravel

Integrating payment gateways into web applications has become an essential part of e-commerce websites. In this tutorial, we will discuss […]

April 3, 2024

Laravel 11 Ajax CRUD Operation Tutorial Example

**Mastering CRUD Operations with Laravel 11 Ajax: A Comprehensive Tutorial** In the world of web development, interaction between the front-end […]

April 3, 2024

Login as Client in Laravel – Login with user id

**Unlock the Power of Laravel with Login as Client – Login with User ID** Laravel, the popular PHP framework, offers […]

April 3, 2024

Digital Marketing Toolkit

Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits

Get Free Access Now