Re: [PHP] greedy preg
From: skate [EMAIL PROTECTED] $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 nameJohn/name filefile1.xml/file xFoobar/x /item item nameBob/name filefile2.xml/file xSomething/x /item item nameGeorge/name filefile3.xml/file xGotcha/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... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] greedy preg
skate wrote: $contents = preg_replace( |item.*?$file.*?/item|si, , $contents ); it's being run on an XML file, where each entry is item../item with a $file pointer in there. it works okay, except for the fact that it deletes that record, and every record before it. i can't figure out why it's being greedy? i know i gotta be missing something real simple... What are the possible values of $file? Are you looking to replace just a specific occurance of a $file between item tags? Maybe this will help: $contents = preg_replace(|item[^]*$file[^]*/item|si,,$contents); or just use the 'U' modifier for ungreedy... -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ PHP|Architect: A magazine for PHP Professionals www.phparch.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
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... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] greedy preg
preg_replace(|item.*?$file.*?/item|si,,$contents) news item titlefhh/title date1060205191/date texthhh/text filexml/news/1060205191.xml/file /item item titlefgjghjh/title date1060205186/date textfgjh/text filexml/news/1060205186.xml/file /item item titlefgjhh/title date1060205182/date textfghh/text filexml/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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
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? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] greedy preg
And what about [^]* -if there are no html tags not thought about that, but XML tags, not HTML tags... does that make any difference? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] greedy preg
- Original Message - From: skate [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Wednesday, August 06, 2003 3:16 PM Subject: [PHP] greedy preg $contents = preg_replace( |item.*?$file.*?/item|si, , $contents ); it's being run on an XML file, where each entry is item../item with a $file pointer in there. it works okay, except for the fact that it deletes that record, and every record before it. i can't figure out why it's being greedy? i know i gotta be missing something real simple... You cannot store file pointers. If you output the variable it's going to look something like Resource ID #1 which is meaningless except to the instance of the script that created it. Or did you mean something different by $file pointer? - Kevin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
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... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] greedy preg
You cannot store file pointers. If you output the variable it's going to look something like Resource ID #1 which is meaningless except to the instance of the script that created it. Or did you mean something different by $file pointer? sorry, wrong words... when i meant $file is equal to something like xml/news/1023823.xml -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] greedy preg
skate wrote: What are the possible values of $file? Are you looking to replace just a specific occurance of a $file between item tags? Maybe this will help: $contents = preg_replace(|item[^]*$file[^]*/item|si,,$contents); or just use the 'U' modifier for ungreedy... i'm looking to replace the entire item/item for that respective file. the file will have a value of whichever file that record points to, and will be unique (xml/news/12312312.xml) should .*? not be ungreedy tho? Yeah, it should be. What kind on content can be around $file and also within the item tag? -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ PHP|Architect: A magazine for PHP Professionals www.phparch.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] greedy preg
And what about [^]* -if there are no html tags skate wrote: 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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] greedy preg
Yeah, it should be. What kind on content can be around $file and also within the item tag? here's a snip of the xml file... it's finding the $file correctly, but deletes all records from the beginning of the file onwards. it leaves the xml file with valid xml ... news at the start... so for the most part the preg is working... it's just being too greedy at the start for whatever reason... news item titlefhh/title date1060205191/date texthhh/text filexml/news/1060205191.xml/file /item item titlefgjghjh/title date1060205186/date textfgjh/text filexml/news/1060205186.xml/file /item item titlefgjhh/title date1060205182/date textfghh/text filexml/news/1060205182.xml/file /item /news -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] greedy preg
What are the possible values of $file? Are you looking to replace just a specific occurance of a $file between item tags? Maybe this will help: $contents = preg_replace(|item[^]*$file[^]*/item|si,,$contents); or just use the 'U' modifier for ungreedy... i'm looking to replace the entire item/item for that respective file. the file will have a value of whichever file that record points to, and will be unique (xml/news/12312312.xml) should .*? not be ungreedy tho? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php