SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    deviding a variable into numbers and the others

    I like to devide a variable into numbers and the others.

    The would-be code below doesn't work correctly but I hope it show what I want.
    Code:
    would-be code
    $myVar ='ab4ged23a9cz8493k';
    
    $numberOnly = numberOnly($myVar);
    $non-numberOnly=on-numberOnly($myVar);
    
    echo $numberOnly ;
    echo $non-numberOnly ;
    
    result
    42398493
    
    abgedaczk

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,084
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    PHP Code:
    $onlyNumbers implode(''array_filter(
      
    explode(''$myVar),
      function(
    $a) { return ctype_digit($a); }
    ));
    $onlyOthers implode(''array_filter(
      
    explode(''$myVar),
      function(
    $a) { return !ctype_digit($a); }
    )); 
    PHP>=5.3+ only
    For PHP<=5.2 use create_function
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Location
    United Kingdom
    Posts
    78
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    PHP Code:
    $onlyNumbers implode(''array_filter(
      
    explode(''$myVar),
      function(
    $a) { return ctype_digit($a); }
    ));
    $onlyOthers implode(''array_filter(
      
    explode(''$myVar),
      function(
    $a) { return !ctype_digit($a); }
    )); 
    PHP>=5.3+ only
    For PHP<=5.2 use create_function
    You can't use the explode() function with an empty delimiter. You'll have to use the str_split() function to make each character in the string a value in an array.
    PHP Code:
    $myVar 'a1b2c3d4e5f6g7h8i9j0';
    $onlyNumbers implode(''array_filter(str_split($myVar), function($a) { return ctype_digit($a); }));
    $onlyOthers implode(''array_filter(str_split($myVar), function($a) { return !ctype_digit($a); })); 

  4. #4
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    code1(ScallioXTX) 
    $onlyNumbers = implode('', array_filter(
      explode('', $myVar),
      function($a) { return ctype_digit($a); }
    ));
    
    code2 (modernW)
    $onlyNumbers = implode('', array_filter(str_split($myVar), function($a) { return ctype_digit($a); }));
    As I test both code1 and code2, they produce syntax error, unexpected T_FUNCTION.

    I can't find what is the cause of the error, Pairing of parenthesis or bracket seems correct.

  5. #5
    SitePoint Wizard frank1's Avatar
    Join Date
    Oct 2005
    Posts
    1,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In around 5 minutes i was able to come up with this
    PHP Code:
    <?php
    $mixed
    ='ab4ged23a9cz8493k';

    //input string 
    //returns only numeric values from the string
    //type 1 to get numbers 2 to get alphabet
    function onlyNumbersOrText($string1,$type){
        
    //converting to array to run foreach,foreach does not work in string like in .net
        
    $splitString=str_split($string1); 
        
        
    $appendValue='';
        
    $functionToUse=($type==1)?'is_numeric':'ctype_alpha';
        
        foreach (
    $splitString as $key=>$value){
            if(
    $functionToUse($value)){
                
    $appendValue .=$value;
            }
        }
    return 
    $appendValue;
    }

    echo 
    onlyNumbersOrText($mixed,1);
    echo 
    onlyNumbersOrText($mixed,2);

    ?>
    i agree there can be lots of improvements to this function but still does to job for now

    If you want to non numbers part(not only text) then have a else part rather than using ctype function and return the else part

    Thanks

  6. #6
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,840
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Try this:

    PHP Code:
    ##########################################
    function just_numbers($str=NULL$numbers=TRUE)
    {
      
    $nums '';
      
    $chrs '';

      for( 
    $i2=0$i2<strlen($str); $i2++ ):

        
    $value=$str[$i2];

        if( 
    is_numeric($value) )
        {
          
    $nums .= $value;
        }
        else
        {
          
    $chrs .= $value;
        }
      endfor;

      return 
    $numbers $nums $chrs;
    }

    $myVar ='ab4ged23a9cz8493k';

    echo 
    '<br />nill == 'just_numbers();
    echo 
    '<br />nums == 'just_numbers($myVar);
    echo 
    '<br />chrs == 'just_numbers($myVarFALSE); 
    #output

    nill ==
    nums == 42398493
    chrs == abgedaczk
    $myVar == ab4ged23a9cz8493k
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  7. #7
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,084
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by dotJoon View Post
    As I test both code1 and code2, they produce syntax error, unexpected T_FUNCTION.
    Probably because you're using PHP <= 5.2, I specifically said it only works on PHP>=5.3 and if you're using an older version you should use create_function instead.

    @modernW ; ah yes, str_split. If I got a dollar for every time I made that mistake
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


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
  •