Position of String Occurence

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tesla

    Position of String Occurence

    Hey guys, I have a string like "lalala: djkahsd : dajkdassd : adasd :"

    Is there a function to find the position of the "I"th occurence of a
    character/string? Like lets say I want to find out the position of the
    second colon. How do I go about that?? I can only find functions that
    return the position of the first or last occurence, but not a variable
    occurence ::sniffles::


  • Pedro Graca

    #2
    Re: Position of String Occurence

    Tesla wrote:[color=blue]
    > Hey guys, I have a string like "lalala: djkahsd : dajkdassd : adasd :"
    >
    > Is there a function to find the position of the "I"th occurence of a
    > character/string? Like lets say I want to find out the position of the
    > second colon. How do I go about that?? I can only find functions that
    > return the position of the first or last occurence, but not a variable
    > occurence ::sniffles::[/color]

    Well, you could explode the string at the colons and get

    $arr[0] = 'lalala';
    $arr[1] = ' djkahsd ';
    $arr[2] = ' dajkdassd';
    $arr[3] = ' adasd ';
    $arr[4] = '';

    <?php
    $string = "lalala: djkahsd : dajkdassd : adasd :";
    $arr = explode(':', $string);
    ?>

    But, if you really want the position of the nth ocurrence of needle in
    haystack, I guess your best bet is to make a function that returns that
    (or false if not enough needles are in haystack)

    or use the array above and get the position as a sum of strlen()s, but I
    prefer a specific function :)


    Happy Coding :)
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • Phillip Parr

      #3
      Re: Position of String Occurence

      Hmm, how about exploding the string by colons then calculating the length of
      the first element to work out the beginning of the second? I'm no expert but
      that's what I came up with...


      "Tesla" <tpha@houston.r r.com> wrote in message
      news:TkcZb.1053 3$jl.9201@fe2.t exas.rr.com...[color=blue]
      > Hey guys, I have a string like "lalala: djkahsd : dajkdassd : adasd :"
      >
      > Is there a function to find the position of the "I"th occurence of a
      > character/string? Like lets say I want to find out the position of the
      > second colon. How do I go about that?? I can only find functions that
      > return the position of the first or last occurence, but not a variable
      > occurence ::sniffles::
      >
      >[/color]


      Comment

      Working...