Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2010
    Posts
    122
    Thanks
    6
    Thanked 0 Times in 0 Posts

    need a help to extarct no`s from string

    hi i need to get extact from the following string using php

    $x="YS727 Matt Black, with YS8102 Matte Green";

    i need to get

    $yy=YS727 and $zz=YS8102

    please help me to solve this problem

  • #2
    Regular Coder
    Join Date
    Sep 2010
    Posts
    122
    Thanks
    6
    Thanked 0 Times in 0 Posts
    $x="YS727 Matt Black, with YS8102 Matte Green";

    i need to get

    matching 727 or matching 8102

    how do i get the matching

  • #3
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    something like this maybe?

    PHP Code:
    <?php
     $x
    ='YS727 Matt Black, with YS8102 Matte Green';

      
    $re1='.*?';    # Non-greedy match on filler
      
    $re2='(\\d+)';    # Integer Number 1

      
    if ($c=preg_match_all ("/".$re1.$re2."/is"$x$matches))
      {
          
    $int1=$matches[1][0];
          echo 
    "Found ($int1) \n";
      }
    ?>
    or

    PHP Code:
    <?php
     $x
    ='YS727 Matt Black, with YS8102 Matte Green';

      
    $re1='.*?';    # Non-greedy match on filler
      
    $re2='(\\d+)';    # Integer Number 1
      
    $re3='.*?';    # Non-greedy match on filler
      
    $re4='(\\d+)';    # Integer Number 2

      
    if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4."/is"$x$matches))
      {
          
    $int1=$matches[1][0];
          
    $int2=$matches[2][0];
          print 
    "matching $int1 and $int2 \n";
      }
    ?>
    Last edited by Arcticwarrio; 03-07-2014 at 03:02 PM.
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #4
    Regular Coder
    Join Date
    May 2011
    Posts
    240
    Thanks
    1
    Thanked 56 Times in 55 Posts
    If the strings you need to match are prefixed with YS, you could use the following pattern, which is easy to understand and fast.

    PHP Code:
    $x "YS727 Matt Black, with YS8102 Matte Green";
    $pattern '#YS(\d+)#';
    if (
    preg_match_all($pattern$x$matches))
    {
        
    $numbers $matches[1];
        
    // print_r($numbers);



  •  

    Posting Permissions

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