subtract string between two tags

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bilibytes
    New Member
    • Jun 2008
    • 128

    subtract string between two tags

    Hi all!

    i am trying to figure how to subtract some string which is between two other strings:

    i have:
    for(lots of times){
    some unuseful text and tags...
    <aloha some=text bbbbbbbbb>string</aloha>
    some text and other tags that i dont need.
    <aloha some=textDiffer ent bbbbbbbb> stringasdf</aloha>
    }


    so i noticed that the tags are partly similar.

    it would then be great to say:

    subtract( $content, startTag='bbb>' , endTag='</aloha>')
    storing the resulting strings into array $content

    if it was a xml document i would parse it, but it is an html with some errors...


    i have found this by googleing:

    Code:
    Answer: How do I extract all text between two keywords like start and end? contributed by Punto
    ($content) = $string =~ m/ start (.*) end /;
    That will get the stuff between " start " and " end " into $content.
    but this doesn't look to be much of PHP...

    if you have any suggestion please let me know

    thankyou

    bili
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    I would suggest using the explode() function.
    Arrays have more powerful manipulation functions than strings.

    Comment

    • bilibytes
      New Member
      • Jun 2008
      • 128

      #3
      thanks for replying, but the problem is that there are man params i would like to pass to the function, not just one as explode accepts.

      explode($string Delimiter, $string).
      it is ok for $string like: asdf | asdf| dfaksdfjh | asdfasdfkjh | sdfg
      then should be done explide(' | ', $string)

      but what if you have : asdf <a first substring <b second substring ?c third substring <b fourth substring ?c

      if i just want to get the values between <b and c? tags, i cant use explode() because it accepts only one delimiter...

      if you have any other solution

      thanyou very much

      bili

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Originally posted by bilibytes
        if i just want to get the values between <b and c? tags, i cant use explode() because it accepts only one delimiter...
        You can use it twice:
        [code=php]
        <?php
        $str = "<a first <b second <c third <d fourth";

        $leading = explode("<b", $str);
        $following = explode("<c", $leading[1]);

        $output = trim($following[1]);

        // $output == second
        ?>
        [/code]
        You get the idea?

        Although, for complex strings you may be better of using regular expressions.
        For example:
        [code=php]
        <?php
        $str = "<a first <b second <c third <d fourth";
        $regex = '/.*\<b (.*?) <c.*/i';

        if(preg_match($ regex, $str, $matches)) {
        echo $matches[1]; // == second
        }
        else {
        echo "No match";
        }
        ?>
        [/code]
        They are not the easiest thing to get used to, but once you do, they can save a lot of time.

        Comment

        • bilibytes
          New Member
          • Jun 2008
          • 128

          #5
          ok, so the expert was right, i just didn't see the potential of his answer.

          but i am trying to learn these regexp, hope i'll do it!

          for all this, thank you very much!

          Comment

          Working...