zdp Posted January 7, 2009 Share Posted January 7, 2009 Ok, so I wasn't able to find an issue similar to what I've been struggling with, so I apologize if it has been answered or is really easy :-P So here's my problem: I am importing a file using file_get_contents and want to replace any text that is inside of a double quote unless the double quote is in a single quote itself. The regex I have now will do the former, but not the latter. The regex I am using is '/"(.+?)"/s', and I am using preg_replace. Here are 2 examples and what their expected results should be: ##div('$$title or "$$title"'); // current results: '$$title or "$view->title"' // expected results: '$$title or "$$title"' ##div("$$title or '$$title'"); // current results: "$view->title or '$view->title'" // expected results: "$view->title or '$view->title'" If you notice the first example, the single quoted string should be ignored, whereas right now it is not. That's what I need to fix. Let me know if this makes sense. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/139808-replacing-content-in-double-quotes-but-not-if-the-doubles-quotes-are-in-quotes/ Share on other sites More sharing options...
effigy Posted January 12, 2009 Share Posted January 12, 2009 This is only a basis as unpaired single quotes (apostrophes) may throw off the detection. <pre> <?php ### $'s escaped for sake of example. $data = <<<DATA ##div('\$\$title or "\$\$title"'); // current results: '\$\$title or "\$view->title"' // expected results: '\$\$title or "\$\$title"' ##div("\$\$title or '\$\$title'"); // current results: "\$view->title or '\$view->title'" // expected results: "\$view->title or '\$view->title'" DATA; echo '<b>Original:</b><hr>'; echo $data; echo '<br><br><b>Split on Double Quote Pairs:</b><hr>'; $pieces = preg_split('/(".*?")/s', $data, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); print_r($pieces); echo '<br><br><b>Double Quote Replacement:</b><hr>'; $singles = 0; foreach ($pieces as &$piece) { $singles += substr_count($piece, "'"); ### No replacement should be made if: ### 1. The line does not start with a double quote; and ### 2. If the number of single quotes are not even (closed). if (!substr($piece, 0, 1) == '"' || $singles % 2) { continue; } $piece = preg_replace('/"(.*?)"/s', '"====="', $piece); } print_r($pieces); echo '<br><br><b>Reassembled:</b><hr>'; echo join('', $pieces); ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/139808-replacing-content-in-double-quotes-but-not-if-the-doubles-quotes-are-in-quotes/#findComment-735491 Share on other sites More sharing options...
printf Posted January 21, 2009 Share Posted January 21, 2009 Something like... untested, but it should work. <?php $text = '##div("$$title1 or \'$$title2\'"); ##div(\'$$title3 or "$$title4"\'); '; echo preg_replace_callback ( '/(".*("(?!\')))/Usi', create_function('$match', 'return str_replace ( "\$\$", "\$view->", $match[0] );'), $text ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/139808-replacing-content-in-double-quotes-but-not-if-the-doubles-quotes-are-in-quotes/#findComment-742252 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.