SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  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)

    the position of "my"

    Code:
    $myVar="This is my book"
    I have a variable named "myVar" like the above.


    The position of "This" is 1 because the first letter "T" of the word "This" comes 1st in the variable.
    The position of "is" is 6 because the first letter "i" of the word "is" comes 6th in the variable.
    The position of "my" is 9 because the first letter "m" of the word "my" comes 9th in the variable.
    The position of "book" is 12 because the first letter "b" of the word "book" comes 12th in the variable.


    The following would-be code doesn't work correctly, but I hope it shows what I want.
    Code:
    code
    $myVar="This is my book"
    
    $find_the_postion_of_my=find_the_postion_of_my($myVar,"my");
    
    echo $find_the_postion_of_my;
    
    result
    9
    How can I get the position "9"?

  2. #2
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,826
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by dotJoon View Post
    Code:
    $myVar="This is my book"
    I have a variable named "myVar" like the above.


    The position of "This" is 1 because the first letter "T" of the word "This" comes 1st in the variable.
    The position of "is" is 6 because the first letter "i" of the word "is" comes 6th in the variable.
    The position of "my" is 9 because the first letter "m" of the word "my" comes 9th in the variable.
    The position of "book" is 12 because the first letter "b" of the word "book" comes 12th in the variable.


    The following would-be code doesn't work correctly, but I hope it shows what I want.
    Code:
    code
    $myVar="This is my book"
    
    $find_the_postion_of_my=find_the_postion_of_my($myVar,"my");
    
    echo $find_the_postion_of_my;
    
    result
    9
    How can I get the position "9"?

    Take a look at PHP Manual: strpos()

    // "http://php.net/manual/en/function.strpos.php"
    PHP Code:
    $mystring 'abc';
    $findme   'a';
    $pos strpos($mystring$findme);

    // Note our use of ===.  Simply == would not work as expected
    // because the position of 'a' was the 0th (first) character.
    if ($pos === false) {
        echo 
    "The string '$findme' was not found in the string '$mystring'";
    } else {
        echo 
    "The string '$findme' was found in the string '$mystring'";
        echo 
    " and exists at position $pos";


  3. #3
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,173
    Mentioned
    454 Post(s)
    Tagged
    8 Thread(s)
    Off Topic:

    Quote Originally Posted by dotJoon View Post
    The position of "This" is 1 because the first letter "T" of the word "This" comes 1st in the variable.
    Just remember that numbering starts from zero, so the first letter's position is 0.


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
  •