Vinze Posted January 5, 2007 Share Posted January 5, 2007 Hey, I use the following code to save all strings found in $code to $strings, and then replacing all strings with _|_|_|_:[code=php:0]$pattern = '[(\'(.*?)\')|("(.*?)")]';preg_match_all($pattern, $code, $strings);$parsed = preg_replace($pattern, '_|_|_|_', $code);[/code]This all works fine, until I get to a string like [code=php:0]echo "bla'bla'bla";[/code], which would become [code=php:0]echo "bla_|_|_|_bla";[/code]. How can I make sure this will become [code=php:0]echo _|_|_|_;[/code]?Thanks in advance.PS. Of course I also want [code=php:0]echo 'bla"bla"bla';[/code] to be replaced by [code=php:0]echo "bla_|_|_|_bla";[/code]. Quote Link to comment Share on other sites More sharing options...
effigy Posted January 5, 2007 Share Posted January 5, 2007 From the looks of it:1. It's OK for singles to be inside of doubles. The doubles take higher precedence and engulf the singles.2. It's [u]not[/u] OK for doubles to be inside of singles, or, in other words, singles cannot engulf doubles.If this is correct, change your singles pattern to disallow doubles inside, going from[tt] \'(.*?)\' [/tt]to[tt] \'([^"]*?)\'[/tt]. Quote Link to comment Share on other sites More sharing options...
Vinze Posted January 6, 2007 Author Share Posted January 6, 2007 [quote author=effigy link=topic=121169.msg497727#msg497727 date=1168033266]From the looks of it:1. It's OK for singles to be inside of doubles. The doubles take higher precedence and engulf the singles.2. It's [u]not[/u] OK for doubles to be inside of singles, or, in other words, singles cannot engulf doubles.If this is correct, change your singles pattern to disallow doubles inside, going from[tt] \'(.*?)\' [/tt]to[tt] \'([^"]*?)\'[/tt].[/quote]You're awesome :D The pattern I used was [code=php:0]$pattern = '[(\'([^\']*?)\')|("(?!\')([^"]*?)")]';[/code]A bit different, I don't know why it works now but I'll read up on that, thanks! Quote Link to comment Share on other sites More sharing options...
effigy Posted January 6, 2007 Share Posted January 6, 2007 Hmmm.... You should only need[tt] (.*?) [/tt]within your single quotes. The pattern won't gobble up any more than 2 single quotes since your quantifier is non-greedy (*?), thus giving you: single_quote, anything_but_a_single_quote, single_quote.I also do not see the need for[tt] (?!\')[/tt], since it would only reject a single quote directly following the first double--perhaps this is my ignorance of your data. Quote Link to comment Share on other sites More sharing options...
Vinze Posted January 7, 2007 Author Share Posted January 7, 2007 [quote author=effigy link=topic=121169.msg498315#msg498315 date=1168115387]Hmmm.... You should only need[tt] (.*?) [/tt]within your single quotes. The pattern won't gobble up any more than 2 single quotes since your quantifier is non-greedy (*?), thus giving you: single_quote, anything_but_a_single_quote, single_quote.I also do not see the need for[tt] (?!\')[/tt], since it would only reject a single quote directly following the first double--perhaps this is my ignorance of your data.[/quote]I don't get it either, I just know it works and that's enough for me ;) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.