SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2013
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question split number string and get required numbers.

    Hi,

    I have text with numbers.

    I want to read text ,find numbers and get required numbers from string.

    For example.

    "my description has 123456789 numbers".

    I want to get first four numbers from above message using php.

    $string = 'my description has 123456789 numbers';
    $number = preg_replace("/[^0-9]/", '', $string); // return 123456789

    I mean I need 1234 from above string.


    Any idea?

    -Thanks

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,223
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Yep, put a limit match on it.
    PHP Code:
    $string 'my description has 123456789 numbers'
    $number preg_replace("/[^0-9]{1,4}/"''$string); // return 1234 

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2013
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Hi,

    I am getting out as 123456789 instead 1234.

    Code:
    $string = 'my description has 123456789 numbers'; 
    $number = preg_replace("/[^0-9]{1,4}/", '', $string); // return 1234 
    
    echo"<br>".$string; // my description has 123456789 numbers
    echo"<br>".$number; // 123456789
    Could please check at your end.

    -Thanks

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,223
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Sigh, sorry, I missed the fact you were using preg_replace instead of preg_match.

    PHP Code:
    $matches = [];
    $string 'my description has 123456789 numbers';

    if (
    preg_match("/[0-9]{4}/"$string$matches)) // find only numbers and collect the first group of 4 numbers
    {
        
    var_dump($matches); // if a match of 4 numbers is found, dump the matches array so you can see you can access it using $matches[0]


  5. #5
    SitePoint Zealot
    Join Date
    Apr 2013
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I try your solution and it gives me result but it remove message.

    Do you have any idea about how to cut number from message.

    I want to cut 123456789 to 1234 and keep rest of message as it is.

    Code:
    <?php
    
    function countDigits($str)
    {
        	$NumberArray = array();
    		$noDigits=0;
    		for ($i=0;$i<strlen($message);$i++) {
    				
    			if (is_numeric($message($i))) {
    				$noDigits++;
    				array_push($NumberArray,$message($i));
    			}
    			//return $noDigits;
    		}
    		echo"<pre>";
    		print_r($NumberArray);
    		echo"</pre>";
    }
    
    function sanitize_message($message,$email = true, $numbers = true, $url = true )
    {
    		//$message = strip_tags( $message );
    		if($email)
    		{
    			//strip email address
    			$email_regexp = "[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,3})";
    			$message = ereg_replace($email_regexp, '', $message);
    		}
    		
    		if($numbers)
    		{
    			/*
    			if (preg_match("/[0-9]{4}/", $message, $matches)) // find only numbers and collect the first group of 4 numbers
    			{
    				var_dump($matches); // if a match of 4 numbers is found, dump the matches array so you can see you can access it using $matches[0]
    			}
    			
    			echo"<pre>";
    			print_r($matches);
    			echo"</pre>";
    			
    			echo"<br>".$matches[0];
    			*/
    			//echo count($numbers);	
    			//strip all numerical values
    			$number_regexp = "[0-9]";
    			$message = ereg_replace($number_regexp,'',$message);
    		}
    		
    		//eliminate url from message
    		if($url)
    		{
    			$url_regexp = "(https?://)?(www\.)?([a-zA-z0-9\.])*[a-zA-Z0-9]*\.[a-z]{2,3}";
    			$message = ereg_replace($url_regexp, '', $message);
    		}
    		
    		//eliminate all the bad words
    		$bad_words = array("f**k", "son of a b***h");
    		$message = str_ireplace($bad_words, '', $message);
    		
    		//eliminate all the email words
    		$email_words = array("skype", ".net", ".com", "org", "biz", "@", "yahoo.com", "gmail", "hotmail", "mail", "e-mail", "msn", "dot","net","com",".org",
    							 ".org",".facebook","facebook","Gmail.com","Ymail.com", "Hotmail.com", "AOL.com ","Easy.com","Sky.com", 
    							 "AEmail4u.com", "Caramail", "Care2.com", 
    							 "Catholic Online", "CentralPets", "Computermail", "DC Email", "Ecology Fund", "E-Mail Anywhere", "EasyPeasy", "Eboxmail.net", 
    							 "EmailAccount", "EmailAccounts4Free", "EmailChoice", "Emailyou", "EmailX.net", "Eudora Web Mail","Everyone.net","Execs2K", 
    							 "FastMail", "FasterMail", "FirstName.com","FlashMail","Fresno Mail","Garfield","Gawab","Glay.org", "GMail", "GMX", "Go.com", 
    							 "Go2Now", "GotGeekMail", "Graffiti.net", "Hello Kitty","HushMail","InBox.com", "JoinMe","JoyMail","JungleMate","Killer",
    							 "KittyMail",
    							 "KuKaMail","Lycos.com", "Mail.com", "Mail2World", "MailCity", "Maktoob","MeowMail", "MyOwnEmail", "MyPersonalEmail", "MyPlace",
    							 "MyRealBox", "MyWay","Netscape Mail","NoPeddlers.com","NZ11.com", "OperaMail","OutGun","ParsMail","Rediff Mail", "SiteWarp",
    							 "Snowboard","Surfy.net","SwissMail","UltimateEmail", "Unlimited Mail","Yahoo",".net"," at "," co "," com "," net ",
    							 " org "," biz "," gmail "," yahoo "," hotmail "," mail "," e-mail "," msn "," dot "," ymail "," hotmail.com "," Easy "," Sky "
    							 ," AEmail4u "," Caramail "," FirstName "," FlashMail "," Rediff "," Everyone "," JoinMe "," JoyMail "," JungleMate
    							 "," Go "," Killer 
    							 "," KuKaMail "," NoPeddlers "," aol "," Care2 " ," Eboxmail "," InBox "," Maktoob "," skype "," com "," aol "," aol 
    							 "," facebook "," HushMail 
    							 "," webmail ",".webmail","webmail"
    							 ," yahoo "," Maktoob "," www  " ,"www"," aol "," aol "," aol "," aol "," aol "," aol "," aol "," aol "," aol "," aol "," aol 
    							 "," aol "," aol "," aol ");
    		$message = str_ireplace($email_words, ' ', $message);
    	
    		//echo"<br>Count Numbers=>".$numberCount = countDigits($message)."<br/>";
    		//echo"<br>Count Numbers=>".$numberCount2 = countDigits2($message)."<br/>";
    		//print count(array_filter(str_split($message),'is_numeric'));
    		
    		return $message;
    }
    
    $MyMessage="
    <p>
    Hi User,
    </p>
    
    <p>
    Mobile number => 123456789
    </p>
    
    <p>
    Mobile number => 987654321
    </p>
    
    <p>
    -Thanks
    </p>
    ";
    
    echo"<br>".$MyMessage."<br>";
    echo"<br>------------------------------------------<br>";
    echo"<br>".$Test2 = sanitize_message($MyMessage)."<br>";
    echo"<br>".$Test3 = countDigits($MyMessage)."<br>";
    
    
    ?>
    Any Idea?

    -Thanks

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,223
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    So you want to take
    PHP Code:
    $string 'my description has 123456789 numbers'
    And turn it into?
    PHP Code:
    $string 'my description has 1234 numbers'

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,223
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    And a follow up question, what would you do if you have to sets of numbers in the string?

    PHP Code:
    $string 'my description has 123456789 numbers and 987654321 in it'
    Would you expect
    PHP Code:
    $string 'my description has 1234 numbers and 9876 in it'
    or
    PHP Code:
    $string 'my description has 1234 numbers and 987654321 in it'

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,223
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Kind of a "rushed" solution and I'm sure there is a more elegant one, but it looks to get the job done.
    PHP Code:
    <?php
    $matches 
    = [];
    $string 'my description has 123456789 numbers';

    if (
    preg_match("|[0-9]+|"$string$matches))
    {
        
    $newString preg_replace("|" preg_quote($matches[0]) . "|"substr($matches[0], 04), $string);
        
    var_dump($newString); // 'my description has 1234 numbers'
    }

    $string 'my description has 123456789 numbers and 987654321 in it';

    if (
    preg_match("|[0-9]+|"$string$matches))
    {
        
    $newString preg_replace("|" preg_quote($matches[0]) . "|"substr($matches[0], 04), $string);
        
    var_dump($newString); // 'my description has 1234 numbers and 987654321 in it'
    }

    $string 'my description has 123456789 numbers and 987654321 in it';

    if (
    preg_match_all("|[0-9]+|"$string$matches))
    {
        
    $newString $string;
        foreach (
    $matches[0] as $match)
            
    $newString preg_replace("|" preg_quote($match) . "|"substr($match04), $newString);
        
    var_dump($newString); // 'my description has 1234 numbers and 9876 in it'
    }

  9. #9
    SitePoint Zealot
    Join Date
    Apr 2013
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Many Many thanks cpradio .

    You are excellent programmer.

    -Thanks

  10. #10
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Code:
    $string = 'my description has 123456789 numbers and 987654321 in it';
    
    if (preg_match("|[0-9]+|", $string, $matches))
    {
        $newString = preg_replace("|" . preg_quote($matches[0]) . "|", substr($matches[0], 0, 4), $string,1);
        var_dump($newString); // 'my description has 1234 numbers and 987654321 in it'
    }
    Needed a slight tweak to prevent recurrance. ("My description has 12345 numbers and 12345 points!" => "My description has 1234 numbers and 12345 points!")
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •