Jump to content

effigy

Staff Alumni
  • Posts

    3,600
  • Joined

  • Last visited

    Never

Posts posted by effigy

  1. Regular expressions are not the best tool for nested data, but this works:

     

    <pre>
    <?php
    
    $data = <<<DATA
    <custom>
        probe1
        <custom>
            probe2
        </custom>
        probe3
    </custom>
    <custom>
        probe4
    </custom>
    <custom>
        probe5
    </custom>
    DATA;
    
    preg_match_all('%<custom>(??!<custom>)(?!</custom>).)*<\/custom>%s', $data, $matches);
    print_r($matches);
    
    ?>
    </pre>
    

  2. I think it's best to include the + since it doesn't hurt. If the data is coming from a user they could have a typo—"02/27//2009" for instance—which, when split, would create 4 elements rather than 3. However, if there are any delimiters on the ends, this would still create empty elements. I recommend:

     

    preg_split('/\D+/', $string, -1, PREG_SPLIT_NO_EMPTY)

  3. use lazyiness

    '/href="(.*?)"/i'

     

    Even better, be specific: '/href="([^"]*)"/i'

     

    in the regex above, you are taking into consideration escaped quotes

     

    The double quotes were escaped in the regex because they were used to delimit the string; they will match an unescaped quote:

     

    <pre>
    <?php
      $str = '"';
      echo preg_match("/\"/", $str);
    ?>
    </pre>
    

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.