Professional Web Applications Themes

[PHP] greedy preg - PHP Development

> preg_replace("|<item>.*?$file.*?</item>|si","",$contents) > <news> > <item> > <title>fhh</title> > <date>1060205191</date> > <text>hhh</text> > <file>xml/news/1060205191.xml</file> > </item> > <item> > <title>fgjghjh</title> > <date>1060205186</date> > <text>fgjh</text> > <file>xml/news/1060205186.xml</file> > </item> > <item> > <title>fgjhh</title> > <date>1060205182</date> > <text>fghh</text> > <file>xml/news/1060205182.xml</file> > </item> > </news> Not sure if I understand you correct but the way you wrote your replace pattern the result should be <news></news>. If you want just the file to be replaced you have to use something like preg_replace("|<item>.*?($file).*?</item>|si",'\1',$contents) Regards Stefan Langer...

  1. #1

    Default Re: [PHP] greedy preg

    > preg_replace("|<item>.*?$file.*?</item>|si","",$contents)
    > <news>
    > <item>
    > <title>fhh</title>
    > <date>1060205191</date>
    > <text>hhh</text>
    > <file>xml/news/1060205191.xml</file>
    > </item>
    > <item>
    > <title>fgjghjh</title>
    > <date>1060205186</date>
    > <text>fgjh</text>
    > <file>xml/news/1060205186.xml</file>
    > </item>
    > <item>
    > <title>fgjhh</title>
    > <date>1060205182</date>
    > <text>fghh</text>
    > <file>xml/news/1060205182.xml</file>
    > </item>
    > </news>
    Not sure if I understand you correct but
    the way you wrote your replace pattern the result should be
    <news></news>.
    If you want just the file to be replaced you have to use something like
    preg_replace("|<item>.*?($file).*?</item>|si",'\\1',$contents)

    Regards
    Stefan Langer
    SLanger@spirit21.de Guest

  2. #2

    Default Re: [PHP] greedy preg

    > Not sure if I understand you correct but
    > the way you wrote your replace pattern the result should be
    > <news></news>.
    > If you want just the file to be replaced you have to use something like
    > preg_replace("|<item>.*?($file).*?</item>|si",'\\1',$contents)
    >
    no, i want just the whole <item></item> replaced for that particular file.
    the pattern i wrote shouldn't... doesn't touch the <news> section at all.

    wouldn't the pattern you wrote replace the whole <item></item> with the file
    tho?


    Skate Guest

  3. #3

    Default Re: [PHP] greedy preg

    $contents = preg_replace( "|<item>.*?".$file.".*?</item>|si", "",
    $contents );

    okay, so i figured out that it's matching the first occurence of <item>
    which will always be the first record and then going on to match the $file
    and deleting everything between. obviously not what i want.

    without giving <item> a unique attribute and without preg_match_all and
    looping through them all, is there any other way to do this? make it find
    the first occurence of <item> previous to $file, rather than from the start?

    i hope this makes sense, and is a little clearer than i've been so far...


    Skate Guest

  4. #4

    Default Re: [PHP] greedy preg

    Do you need to use ".*?"? If there will be only white characters, use
    \s* instead.

    skate wrote:
    > $contents = preg_replace( "|<item>.*?".$file.".*?</item>|si", "",
    > $contents );
    >
    > okay, so i figured out that it's matching the first occurence of <item>
    > which will always be the first record and then going on to match the $file
    > and deleting everything between. obviously not what i want.
    >
    > without giving <item> a unique attribute and without preg_match_all and
    > looping through them all, is there any other way to do this? make it find
    > the first occurence of <item> previous to $file, rather than from the start?
    >
    > i hope this makes sense, and is a little clearer than i've been so far...
    >
    >
    >
    Marek Kilimajer Guest

  5. #5

    Default Re: [PHP] greedy preg


    > Do you need to use ".*?"? If there will be only white characters, use
    > \s* instead.
    >

    unfortunately there's some content either side of $file before <item></item>
    this content is different for each item, so i can't define it in the
    pattern.


    Skate Guest

  6. #6

    Default Re: [PHP] greedy preg

    From: "skate" <rootfatcuban.com>
    > $contents = preg_replace( "|<item>.*?".$file.".*?</item>|si", "",
    > $contents );
    >
    > okay, so i figured out that it's matching the first occurence of <item>
    > which will always be the first record and then going on to match the $file
    > and deleting everything between. obviously not what i want.
    >
    > without giving <item> a unique attribute and without preg_match_all and
    > looping through them all, is there any other way to do this? make it find
    > the first occurence of <item> previous to $file, rather than from the
    start?
    >
    > i hope this makes sense, and is a little clearer than i've been so far...
    This example works... adapt to your needs. (Sorry for the delay)

    <?php

    $xml = "
    <item>
    <name>John</name>
    <file>file1.xml</file>
    <x>Foobar</x>
    </item>
    <item>
    <name>Bob</name>
    <file>file2.xml</file>
    <x>Something</x>
    </item>
    <item>
    <name>George</name>
    <file>file3.xml</file>
    <x>Gotcha</x>
    </item>
    ";

    $file = 'file2.xml';

    function callback($matches)
    {
    global $file;
    if(strpos($matches[1],$file) === FALSE)
    { return $matches[0]; }
    else
    { return ''; }
    }

    $new_xml = preg_replace_callback('|<item>(.*)</item>|Usi','callback',$xml);

    echo nl2br(htmlentities($xml));
    echo "<hr>";
    echo nl2br(htmlentities($new_xml));

    ?>

    ---John Holmes...

    Cpt John W. Holmes Guest

Similar Threads

  1. PREG-pattern, help needed
    By Tobias Talltorp in forum PHP Development
    Replies: 2
    Last Post: September 11th, 03:02 PM
  2. ereg & preg
    By Eddy-Das in forum PHP Development
    Replies: 0
    Last Post: August 15th, 03:49 PM
  3. greedy preg
    By Skate in forum PHP Development
    Replies: 5
    Last Post: August 6th, 10:35 PM
  4. Too greedy, my "\d+" but not too greedy to troll
    By Purl Gurl in forum PERL Miscellaneous
    Replies: 1
    Last Post: June 24th, 09:55 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139