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

Thread: Help

  1. #1
    New Coder
    Join Date
    Jun 2002
    Location
    United States
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help

    Could someone pleasse show me a piece of code of how to search a string for the first numerical value and return how many characters from the begininng it is
    example:

    "abcdefg4hi"

    a = 0
    b = 1
    c = 2
    d = 3
    e = 4
    f = 5
    g = 6
    4 = 7th characters from the beginning


    so it would return 7

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    U.S. (Wish Japan though)
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, in C++, you would treat the string the same as an array.

    So:

    PHP Code:
    <?
    $myString
    ="abcdefg4hi";
    for(
    $i=0;$i<$myString.length();$i++)
    {
     if(
    $myString[$i]>=&& $myString[$i]<=9)
    {
    echo(
    "$i is the first character that is a number.");
    }
    }
    ?>
    Again, this is going off of C++ form, with a small amount of modifying. I am unsure as to whether this will work or not.

    Also, if you only want it to print the first one, I think you would add a break; after the echo.

    I hope this helps

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    U.S. (Wish Japan though)
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, well I tested my code, sand it doesn't work. Does anyone know how to get the integer value of a strings length? (EX: If $myString="abcd" the value would be 4....)

    I know how to in C++, but not in PHP..

  • #4
    Regular Coder
    Join Date
    May 2002
    Location
    London, England
    Posts
    369
    Thanks
    0
    Thanked 0 Times in 0 Posts

    here....

    OK, you use strlen() to find the string length....

    PHP Code:
    <?
    $int 
    strlen("abcd");
    echo 
    $int//gives 4
    ?>
    Jee
    Jeewhizz - MySQL Moderator
    http://www.sitehq.co.uk
    PHP and MySQL Hosting

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Use the Power of Regular Expressions:

    PHP Code:
    $numberFound preg_match("/\d/"$myString$arr);
    $offset strpos($myString$arr[0]);
    echo 
    $offset
    BTW: Cloudski, I'm not all to sure that using the comparison operators on strings will always yield desired results because of PHPs inherent type juggling features. Also, you either have to use $myString{$i} to get at the character at that offset or to transform the string into an array via preg_split.

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Location
    U.S. (Wish Japan though)
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, thanks for the information all... I only have experience in C++, so I don't know how PHP handles everything... yet.... *sigh* I still got a long way to go before I give back to this forum.

    Thanks All for helping me help this person!


  •  

    Posting Permissions

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