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 funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts

    Challenge: Creating int from mixed string

    You might consider this task easy or hard but i don't know, it just came cross my mind.

    This is the task.

    1) you have a variable that is a string $string ="aB%3C%'B\^B*3%5SDb";

    2) extract numbers from string called $number

    3) check those numbers with this solution $solution = "335";

    if this structure echoes true you have a solution

    PHP Code:
    if ((int)$number == (int)$solution) {
     echo 
    "true";
    }
    else{
     echo 
    "false";

    first to solve gets thanks!

  • #2
    New Coder
    Join Date
    Apr 2008
    Location
    Blagoevgrad, Bulgaria
    Posts
    47
    Thanks
    0
    Thanked 8 Times in 8 Posts
    Something like this maybe:

    PHP Code:
    $string "aB%3C%'B\^B*3%5SDb";
    $numbers preg_replace("/[^0-9]/"""$string); 
    or

    PHP Code:
    $string "aB%3C%'B\^B*3%5SDb";

    $count strlen($string);

    for (
    $i 0$i $count$i++) {
      if (
    is_numeric($string[$i])) {
        
    $numbers .= $string[$i];
      }
    }

    settype($numbers"integer");

    echo 
    $numbers
    Last edited by er4o; 10-22-2009 at 04:21 AM.

  • Users who have thanked er4o for this post:

    funnymoney (10-22-2009)

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Eh, not the first, but I have a simpler regex than was already offered, just for the heck of it:
    PHP Code:
    $number preg_replace'/\D/'''$string ); 
    Very simple... homework assignment?
    Last edited by kbluhm; 10-22-2009 at 07:24 AM.

  • Users who have thanked kbluhm for this post:

    funnymoney (10-22-2009)

  • #4
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    Excellent! No it's not homework, just something i made up. Thought it's gonna be more complicated. I'm off thinking some tougher challanges, and will be back with questions


  •  

    Posting Permissions

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