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 9 of 9

Thread: php substring

  1. #1
    Regular Coder howie2009's Avatar
    Join Date
    May 2009
    Location
    Ireland
    Posts
    202
    Thanks
    37
    Thanked 0 Times in 0 Posts

    php substring

    Hi Guys,

    whats the php substring code to extract and echo all the digits after ID:

    eg

    full string is "John Doe - ID: 557675567567"
    full string is " Jake Lee - ID: 1234A3"

    etc..

    I need to extract all the characters after ID: for varying numbers of characters eliminating any whitespace issues so in the first case my desired result would be 557675567567 and in the 2nd its 1234A3

    Thanks

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    try

    PHP Code:
    <?php
      
    # URL that generated this code:
      # http://txt2re.com/index-php.php3?s=John%20Doe%20-%20ID:%20557675567567&1

      
    $txt='John Doe - ID: 557675567567';

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

      
    if ($c=preg_match_all ("/".$re1.$re2."/is"$txt$matches))
      {
          
    $int1=$matches[1][0];
          print 
    "($int1) \n";
      }

      
    #-----
      # Paste the code into a new php file. Then in Unix:
      # $ php x.php 
      #-----
    ?>
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #3
    Regular Coder howie2009's Avatar
    Join Date
    May 2009
    Location
    Ireland
    Posts
    202
    Thanks
    37
    Thanked 0 Times in 0 Posts
    Thanks very much. Should the id use a non numeric character how would I tweak the code please?

    Eg John Doe - ID: 557ABCtux@$675567567

  • #4
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    which part of John Doe - ID: 557ABCtux@$675567567 do you want to extract?3

    just exerything after ID: ?
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #5
    Regular Coder howie2009's Avatar
    Join Date
    May 2009
    Location
    Ireland
    Posts
    202
    Thanks
    37
    Thanked 0 Times in 0 Posts
    Thanks found a script

    Code:
    <?php
    $str = "John Doegdfgf - ID: 12545981QA78453478%^$^&^";
     if (($tmp = strstr($str, 'ID:')) !== false) {$str = substr($tmp, 1);
     echo $str;} ?>

  • #6
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    PHP Code:
    $mystring 'John Doe - ID:  557ABCtux@$675567567';
    $foundit substr($mystring strpos($mystring'ID:')+4);
    echo 
    $foundit
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • Users who have thanked Arcticwarrio for this post:

    howie2009 (03-04-2013)

  • #7
    Regular Coder howie2009's Avatar
    Join Date
    May 2009
    Location
    Ireland
    Posts
    202
    Thanks
    37
    Thanked 0 Times in 0 Posts
    Perfect I will use that instead of the one I found..Thanks

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    If you always use the same syntax: firstName lastName - ID: XXX including all the spacing, you can use scanf to get what you need as well:
    PHP Code:
        list($fname$lname$id) = sscanf($itm'%s %s - ID: %s');
        
    printf("Name: %s %s, id: %s" PHP_EOL$fname$lname$id); 
    Where $itm is the string. $id will contain just the id portion. You can assign directly to an array if you want instead of extracting the variables with the list.
    Overall, the substr and strpos will be faster than a scanf (albeit nominal difference overall), but the scanf does provide multiple return results. So if you can make use of the first and last name as well as the id, then a scanf would be more suitable. If you only want the id and have no need for any of the other data, than use a substr.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #9
    New to the CF scene
    Join Date
    Feb 2013
    Location
    India
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You have shared really very helpful information which is helped me so much and I think it will help to many other people.


  •  

    Posting Permissions

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