expression problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • emmerink@gmail.com

    #1

    expression problem

    Hi,

    $xml = "tekst <titletwee </title>\n drie </trvier </td>";

    $a_sSubject = trim($xml);

    $a_sStart = '<title>';$a_sE nd = '<\/tr>';
    $pattern = '/'. $a_sStart .'(.*?)'. $a_sEnd .'/';
    preg_match_all( $pattern, $a_sSubject, $result);

    print_r($result );



    results in:
    Array
    (
    [0] =Array
    (
    )

    [1] =Array
    (
    )

    )

    Any idea why there is no content in the result?
    (expected to find the content in between <titleen </tr>)

    Thanks for any help.

  • naixn

    #2
    Re: expression problem

    emmerink@gmail. com wrote :
    Hi,
    >
    $xml = "tekst <titletwee </title>\n drie </trvier </td>";
    >
    $a_sSubject = trim($xml);
    >
    $a_sStart = '<title>';$a_sE nd = '<\/tr>';
    $pattern = '/'. $a_sStart .'(.*?)'. $a_sEnd .'/';
    preg_match_all( $pattern, $a_sSubject, $result);
    >
    print_r($result );
    >
    >
    >
    results in:
    Array
    (
    [0] =Array
    (
    )
    >
    [1] =Array
    (
    )
    >
    )
    >
    Any idea why there is no content in the result?
    (expected to find the content in between <titleen </tr>)
    >
    Thanks for any help.
    >
    Why use (.*?) ?
    That means : search for any string with 0 or more characters. And the
    '?' is here to say 'but this is not mandatory'.
    That means : 0 ore more characters, or 0 characters.

    Now:

    <?php
    $xml = "tekst <titletwee </title>\n drie </trvier </td>";

    $a_sSubject = trim($xml);

    $a_sStart = '<title>';
    $a_SEnd = '</tr>';
    $pattern = '!' . $a_sStart . '(.*)' . $a_sEnd . '!';
    preg_match_all( $pattern, $a_sSubject, $result);

    print_r($result );
    ?>


    That worked for me (using '!' instead of '/' as regexp delimiter).
    You may try to play 's' and/or 'm' regexp modifier to match the newline
    character.

    --
    Naixn

    Comment

    • BKDotCom

      #3
      Re: expression problem

      emmerink@gmail. com wrote:
      $xml = "tekst <titletwee </title>\n drie </trvier </td>";
      >
      $a_sSubject = trim($xml);
      >
      $a_sStart = '<title>';$a_sE nd = '<\/tr>';
      $pattern = '/'. $a_sStart .'(.*?)'. $a_sEnd .'/';
      preg_match_all( $pattern, $a_sSubject, $result);
      >
      print_r($result );
      >
      Any idea why there is no content in the result?
      (expected to find the content in between <titleen </tr>)
      pay no attention to naixn. :) The ? character != "ignore" it means
      "not greedy"
      Your "problem" is that your search string contains a linebreak.
      you need to add the "s" modifier to your expression so that the "."
      matches the "\n"

      $pattern = '/'. $a_sStart .'(.*?)'. $a_sEnd .'/s'; // should be good

      Comment

      • emmerink@gmail.com

        #4
        Re: expression problem


        BKDotCom schreef:
        emmerink@gmail. com wrote:
        $xml = "tekst <titletwee </title>\n drie </trvier </td>";

        $a_sSubject = trim($xml);

        $a_sStart = '<title>';$a_sE nd = '<\/tr>';
        $pattern = '/'. $a_sStart .'(.*?)'. $a_sEnd .'/';
        preg_match_all( $pattern, $a_sSubject, $result);

        print_r($result );

        Any idea why there is no content in the result?
        (expected to find the content in between <titleen </tr>)
        >
        pay no attention to naixn. :) The ? character != "ignore" it means
        "not greedy"
        Your "problem" is that your search string contains a linebreak.
        you need to add the "s" modifier to your expression so that the "."
        matches the "\n"

        $pattern = '/'. $a_sStart .'(.*?)'. $a_sEnd .'/s'; // should be good
        Thanks, that did it. Although naixn noticed the modifier, you were
        much more to the point (which made it more useful in this quite
        confusing regexp notations).

        Comment

        • naixn

          #5
          Re: expression problem

          BKDotCom wrote :
          emmerink@gmail. com wrote:
          >$xml = "tekst <titletwee </title>\n drie </trvier </td>";
          >>
          >$a_sSubject = trim($xml);
          >>
          >$a_sStart = '<title>';$a_sE nd = '<\/tr>';
          >$pattern = '/'. $a_sStart .'(.*?)'. $a_sEnd .'/';
          >preg_match_all ($pattern, $a_sSubject, $result);
          >>
          >print_r($resul t);
          >>
          >Any idea why there is no content in the result?
          >(expected to find the content in between <titleen </tr>)
          >
          pay no attention to naixn. :) The ? character != "ignore" it means
          "not greedy"
          Your "problem" is that your search string contains a linebreak.
          you need to add the "s" modifier to your expression so that the "."
          matches the "\n"

          $pattern = '/'. $a_sStart .'(.*?)'. $a_sEnd .'/s'; // should be good
          >
          I didn't say that ? == 'ignore', but 0 or 1 :p
          But I didn't know it was able to manage greediness. I learnt some more regexp
          today =D. Thanks ;)

          --
          Naixn

          Comment

          • Curtis

            #6
            Re: expression problem

            You should preg_quote() the vars being inserted into the regex.
            Although you know what's in them now, it would make it easier to change
            them at a later time, without the need to statically escape.

            Comment

            • Dave Benjamin

              #7
              Re: expression problem

              Curtis wrote:
              You should preg_quote() the vars being inserted into the regex.
              Although you know what's in them now, it would make it easier to change
              them at a later time, without the need to statically escape.
              Excellent advice.

              Comment

              Working...