need function: split string by list of strings and return delimiters and extra text

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Justin L. Kennedy

    need function: split string by list of strings and return delimiters and extra text

    I am looking for a function that takes in a string and splits it using a
    list of other strings (delimiters) and can return the delimiters as well
    as the extra parts of the string. I was trying the split with a regex
    built up of the delimiters separated by "|", but it doesn't return the
    delimiters which I need.

    The goal is an algorithm that can take a string containing html code using
    <ul>, <ol>, and <li> and turn it into a formated plain text form like the
    following:

    <ol>
    <li>foo</li>
    <li>bar</li>
    <ul>
    <li>baz</li>
    <li>qux</li>
    </ul>
    <li>asf</li>
    </ol>

    into:
    1) foo
    2) bar
    * baz
    * qux
    3) asf

    I have everything but tokenizing the html input. I'm sure there is
    probably a function in the PHP 4.3.4 libraries that I am missing, but I
    can't seem to find it.

    --
    Justin L. Kennedy
    Georgia Institute of Technology, Atlanta Georgia, 30332
    Email: jk289@prism.gat ech.edu
  • Brion Vibber

    #2
    Re: need function: split string by list of strings and return delimitersand extra text

    Justin L. Kennedy wrote:[color=blue]
    > I am looking for a function that takes in a string and splits it using a
    > list of other strings (delimiters) and can return the delimiters as well
    > as the extra parts of the string. I was trying the split with a regex
    > built up of the delimiters separated by "|", but it doesn't return the
    > delimiters which I need.[/color]

    You might try preg_match_all( ).
    [color=blue]
    > The goal is an algorithm that can take a string containing html code using
    > <ul>, <ol>, and <li> and turn it into a formated plain text form like the
    > following:[/color]

    Note that HTML is really tough to reliably parse with regular
    expressions. If your source material is reasonably regular this might be
    'good enough' though.

    -- brion vibber (brion @ pobox.com)

    Comment

    • Chung Leong

      #3
      Re: need function: split string by list of strings and return delimiters and extra text

      "Justin L. Kennedy" <jk289@prism.ga tech.edu> wrote in message
      news:ctrd43$b2k $1@news-int.gatech.edu. ..[color=blue]
      > I am looking for a function that takes in a string and splits it using a
      > list of other strings (delimiters) and can return the delimiters as well
      > as the extra parts of the string. I was trying the split with a regex
      > built up of the delimiters separated by "|", but it doesn't return the
      > delimiters which I need.[/color]

      preg_split() let you do capturing, I think. Check the manual.


      Comment

      • Justin L. Kennedy

        #4
        Re: need function: split string by list of strings and return delimiters and extra text

        Chung Leong <chernyshevsky@ hotmail.com> wrote:[color=blue]
        > "Justin L. Kennedy" <jk289@prism.ga tech.edu> wrote in message
        > news:ctrd43$b2k $1@news-int.gatech.edu. ..[color=green]
        >> I am looking for a function that takes in a string and splits it using a
        >> list of other strings (delimiters) and can return the delimiters as well
        >> as the extra parts of the string. I was trying the split with a regex
        >> built up of the delimiters separated by "|", but it doesn't return the
        >> delimiters which I need.[/color][/color]
        [color=blue]
        > preg_split() let you do capturing, I think. Check the manual.[/color]

        Thanks, that was the one I was looking for. I was only looking in the
        array and string sections of the manual.

        --
        Justin L. Kennedy
        Georgia Institute of Technology, Atlanta Georgia, 30332
        Email: jk289@prism.gat ech.edu

        Comment

        Working...