regular expression

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

    regular expression

    preg_match("/<some_tag >([^']*?)<\/some_tag>/", $data, $matches)
    preg_match_all( "/<p>(.*?)<\/p>/", $matches[1], $paragraphs);

    foreach ($paragraphs[0] as $paragraph) {
    $content=$conte nt.$paragraph;
    }

    The above code only works if <pis the first tag under <some_tag>. i.e,
    works with

    <some_tag >
    <p>blah</p>
    </some_tag>

    but not with

    <some_tag >
    <some_other_tag >blah</some_other_tag>
    <p>blah</p>
    </some_tag>


    How could I make the code works regardless of the position of <punder
    <some_tag>? Thank you.


  • Jerry Stuckle

    #2
    Re: regular expression

    Peter Parker wrote:
    preg_match("/<some_tag >([^']*?)<\/some_tag>/", $data, $matches)
    preg_match_all( "/<p>(.*?)<\/p>/", $matches[1], $paragraphs);
    >
    foreach ($paragraphs[0] as $paragraph) {
    $content=$conte nt.$paragraph;
    }
    >
    The above code only works if <pis the first tag under <some_tag>. i.e,
    works with
    >
    <some_tag >
    <p>blah</p>
    </some_tag>
    >
    but not with
    >
    <some_tag >
    <some_other_tag >blah</some_other_tag>
    <p>blah</p>
    </some_tag>
    >
    >
    How could I make the code works regardless of the position of <punder
    <some_tag>? Thank you.
    >
    >
    Try alt.html. This isn't a PHP problem.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Curtis

      #3
      Re: regular expression

      Jerry Stuckle wrote:
      Peter Parker wrote:
      >preg_match("/<some_tag >([^']*?)<\/some_tag>/", $data, $matches)
      >preg_match_all ("/<p>(.*?)<\/p>/", $matches[1], $paragraphs);
      >>
      >foreach ($paragraphs[0] as $paragraph) {
      > $content=$conte nt.$paragraph;
      > }
      >>
      >The above code only works if <pis the first tag under <some_tag>.
      >i.e, works with
      >>
      ><some_tag >
      > <p>blah</p>
      > </some_tag>
      >>
      >but not with
      >>
      ><some_tag >
      > <some_other_tag >blah</some_other_tag>
      > <p>blah</p>
      ></some_tag>
      >>
      >>
      >How could I make the code works regardless of the position of <p>
      >under <some_tag>? Thank you.
      >>
      >>
      >
      Try alt.html. This isn't a PHP problem.
      >
      How is it not a PHP problem? The OP is asking about using regex in PHP.

      Anyway, @OP: You might be able to get what you're after with one
      regex. Check out the print_r output to figure out what you need, and
      if it works:

      $data = '<div>Foo bar <p><em>baz</em></pIshmael</div>';

      // use a different delimiter when / occurs, to avoid
      // unnecessary escaping
      $re = '%<([\w.:-]+)[^<>]*>(.*?<p[^<>]*>(.*?)</p>.*?)</\1>%i';

      preg_match_all( $re, $data, $matches);

      print_r($matche s);

      --
      Curtis, http://dyersweb.com

      Comment

      • Jerry Stuckle

        #4
        Re: regular expression

        Curtis wrote:
        Jerry Stuckle wrote:
        >Peter Parker wrote:
        >>preg_match( "/<some_tag >([^']*?)<\/some_tag>/", $data, $matches)
        >>preg_match_al l("/<p>(.*?)<\/p>/", $matches[1], $paragraphs);
        >>>
        >>foreach ($paragraphs[0] as $paragraph) {
        >> $content=$conte nt.$paragraph;
        >> }
        >>>
        >>The above code only works if <pis the first tag under <some_tag>.
        >>i.e, works with
        >>>
        >><some_tag >
        >> <p>blah</p>
        >> </some_tag>
        >>>
        >>but not with
        >>>
        >><some_tag >
        >> <some_other_tag >blah</some_other_tag>
        >> <p>blah</p>
        >></some_tag>
        >>>
        >>>
        >>How could I make the code works regardless of the position of <p>
        >>under <some_tag>? Thank you.
        >>>
        >>>
        >>
        >Try alt.html. This isn't a PHP problem.
        >>
        >
        How is it not a PHP problem? The OP is asking about using regex in PHP.
        >
        Anyway, @OP: You might be able to get what you're after with one regex.
        Check out the print_r output to figure out what you need, and if it works:
        >
        $data = '<div>Foo bar <p><em>baz</em></pIshmael</div>';
        >
        // use a different delimiter when / occurs, to avoid
        // unnecessary escaping
        $re = '%<([\w.:-]+)[^<>]*>(.*?<p[^<>]*>(.*?)</p>.*?)</\1>%i';
        >
        preg_match_all( $re, $data, $matches);
        >
        print_r($matche s);
        >
        --
        Curtis, http://dyersweb.com
        Yep, you're right - I misread the question. Sorry.

        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        Working...