#1
  1. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,660
    Rep Power
    4123

    Confused - one regex works, bit a similar one does not work


    I'm using preg_replace() in PHP

    I have a template file that looks like

    Code:
    Some text here
    {%%resources
    	<li>/*resource*/</li>
    %%}
    some more text here
    I have a pattern that looks like

    PHP Code:
    $pattern '/\/\*(.+?)\*\//'
    using preg_replace() this pattern will match /*resource*/ in the above example

    however, I also want to be able to find {%% to %%}, so i tried

    PHP Code:
    $pattern '/{%%(.+?)%%}/'
    but that didn't work, so i tried escaping the % symbols - no luck. Then i tried escaping the { and } instead - no luck. I tried escaping all three characters in the delimiters, but with no luck

    What am I doing wrong?
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    Try:
    Originally Posted by Northie
    PHP Code:
    $pattern '/{%%(.+?)%%}/s'
    With the proviso that I don't actually use PHP, I believe the dot character doesn't match newlines by default (it certainly doesn't in Perl, on whose regexp engine PHP's is based). You need the /s modifier to indicate that you want to treat your string as a single line.

    Comments on this post

    • Northie agrees : many thanks
  4. #3
  5. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,660
    Rep Power
    4123
    That worked a treat, thanks

    I was two chapters away from reading the "modifiers" section of http://www.regular-expressions.info/ and had got my version to

    Code:
    '/[{%%][^%%}]+?[%%}]/'
    which left me with

    Code:
    {%%}
    which I just could not explain
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]

IMN logo majestic logo threadwatch logo seochat tools logo