Full-Demon Posted October 11, 2007 Share Posted October 11, 2007 Hi, I'm trying to parse a file, and I only need to replace stuff that is outside any single or double quotes. How can I, using preg_match, see if the found key is between quotes or not? Thank you! FD Quote Link to comment https://forums.phpfreaks.com/topic/72739-preg-checking-if-match-is-between-quotes/ Share on other sites More sharing options...
effigy Posted October 11, 2007 Share Posted October 11, 2007 preg_split on /"(.*?)"/ and perform the operations on elements that do not begin with ". Quote Link to comment https://forums.phpfreaks.com/topic/72739-preg-checking-if-match-is-between-quotes/#findComment-367069 Share on other sites More sharing options...
Rithiur Posted October 11, 2007 Share Posted October 11, 2007 Well.. to do exactly what you want, you can do something like this: $string = preg_replace('/\G(([^\'"]*|([\'"]).*\3)*)foo/Us', '$1bar', $string); That will replace all 'foo's that are not inside single or double quotes with 'bar's. Of course, do remeber, that if you are trying to match strings that contain text, the quotes used in words like "don't" also count. Quote Link to comment https://forums.phpfreaks.com/topic/72739-preg-checking-if-match-is-between-quotes/#findComment-367083 Share on other sites More sharing options...
effigy Posted October 11, 2007 Share Posted October 11, 2007 PCRE does not support \G. Quote Link to comment https://forums.phpfreaks.com/topic/72739-preg-checking-if-match-is-between-quotes/#findComment-367086 Share on other sites More sharing options...
Rithiur Posted October 11, 2007 Share Posted October 11, 2007 PCRE does not support \G. To quote the PHP manual: The \G assertion is true only when the current matching position is at the start point of the match, as specified by the offset argument of preg_match(). It differs from \A when the value of offset is non-zero. It is available since PHP 4.3.3. Quote Link to comment https://forums.phpfreaks.com/topic/72739-preg-checking-if-match-is-between-quotes/#findComment-367094 Share on other sites More sharing options...
effigy Posted October 11, 2007 Share Posted October 11, 2007 I was looking at this in the manual: The Perl \G assertion is not supported as it is not relevant to single pattern matches. That's confusing. Quote Link to comment https://forums.phpfreaks.com/topic/72739-preg-checking-if-match-is-between-quotes/#findComment-367102 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.