abazoskib Posted October 14, 2009 Share Posted October 14, 2009 I am still getting the hang of regex expressions, but I cant seem to figure out how I would escape quoted text within a string. The qualitifcations for escaping them would be to have [a space] [a double quote] [any text] [a double quote] [a space] but there cant be anything besides a space before and after, not even a newline. here's what i have so far: $pattern = '/ "." /'; $replacement = '/ \".\" /'; $val = preg_replace($pattern, $replacement, $val); But it's not working. I've always wanted to learn how to use regular expressions and like I said I am new to them. Quote Link to comment https://forums.phpfreaks.com/topic/177705-solved-regex-for-quoted-text-within-a-string/ Share on other sites More sharing options...
simshaun Posted October 14, 2009 Share Posted October 14, 2009 Pattern: \s"(.*)"\s Replacement: \"\1\" PHP: $result = preg_replace('/\s"(.*)"\s/i', ' \"\" ', $subject); Quote Link to comment https://forums.phpfreaks.com/topic/177705-solved-regex-for-quoted-text-within-a-string/#findComment-937003 Share on other sites More sharing options...
abazoskib Posted October 14, 2009 Author Share Posted October 14, 2009 Thanks simshaun, however the text in between the quotes disappears after using the replacement you gave me. edit: Got it..thanks a lot! $subval = preg_replace('/\s"(.*)"\s/i', ' \"\1\"', $subval); Quote Link to comment https://forums.phpfreaks.com/topic/177705-solved-regex-for-quoted-text-within-a-string/#findComment-937007 Share on other sites More sharing options...
Psycho Posted October 14, 2009 Share Posted October 14, 2009 That won't work simshaun. The '*' by itself is greedy and will match the text between the very first and very last double quote instead of matching multiple quoted strings when they exist. Plus it REMOVED the quoted text entirely instead of just escaping the double quote marks. $val = 'Here is "quote number one" and here is "quote number two" with a space'; $pattern = '/\s\"(.*?)\"\s/'; $replacement = ' \\\"\1\\\" '; $val = preg_replace($pattern, $replacement, $val); echo $val; //output: // Here is \"quote number one\" and here is \"quote number two\" with a space Quote Link to comment https://forums.phpfreaks.com/topic/177705-solved-regex-for-quoted-text-within-a-string/#findComment-937008 Share on other sites More sharing options...
simshaun Posted October 14, 2009 Share Posted October 14, 2009 That won't work simshaun. The '*' by itself is greedy and will match the text between the very first and very last double quote instead of matching multiple quoted strings when they exist.Yes, I should have made it nongreedy, but the entire logic is flawed in the first place. What if the string is ended with quoted text? Is it not supposed to be escaped as well? Plus it REMOVED the quoted text entirely instead of just escaping the double quote marks.You must have looked at it roughly 10 or 20 seconds after I posted, because I fixed it shortly after posting. Quote Link to comment https://forums.phpfreaks.com/topic/177705-solved-regex-for-quoted-text-within-a-string/#findComment-937014 Share on other sites More sharing options...
simshaun Posted October 14, 2009 Share Posted October 14, 2009 Also, none of the regexes posted work with nested quotes. Quote Link to comment https://forums.phpfreaks.com/topic/177705-solved-regex-for-quoted-text-within-a-string/#findComment-937017 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.