rarebit Posted December 13, 2008 Share Posted December 13, 2008 Hi, Whats the best way to extract quoted strings? Given the following example: this isn't an "interesting example", but 'then what' would we expect? The main problem is the single quote in "isn't", can this be (generally) caught because in this type of situation it is concatenated between other characters, whereas a string generally has whitespace on at least one side. This however wouldn't catch situations like: I saw 'em 'ther day like... Any suggestions? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/136864-solved-extract-quoted-strings/ Share on other sites More sharing options...
DarkWater Posted December 13, 2008 Share Posted December 13, 2008 How about: <?php $string = <<<STRING this isn't an "interesting example", but 'then what' would we expect? STRING; preg_match_all('/\B(["\'])(.+?)\1\B/s', $string, $matches); print_r($matches); Worked for me. Quote Link to comment https://forums.phpfreaks.com/topic/136864-solved-extract-quoted-strings/#findComment-714791 Share on other sites More sharing options...
rarebit Posted December 13, 2008 Author Share Posted December 13, 2008 I likes it, a lot! Never used 'back references' before (ref), very useful. As expected the following fails a little... $string = "I saw 'em 'ther day like 'had bells' on they did \"that arh\" y'know! But 'hey what's thee' expect like?"; print $string."<br>"; preg_match_all('/\B(["\'])(.+?)\1\B/s', $string, $matches); print_r($matches[0]); VERY GOOD, CHEERS MR dARKwATER! Quote Link to comment https://forums.phpfreaks.com/topic/136864-solved-extract-quoted-strings/#findComment-714804 Share on other sites More sharing options...
DarkWater Posted December 13, 2008 Share Posted December 13, 2008 The good part is that it didn't fail on 'hey what's thee'. Quote Link to comment https://forums.phpfreaks.com/topic/136864-solved-extract-quoted-strings/#findComment-714810 Share on other sites More sharing options...
rarebit Posted December 13, 2008 Author Share Posted December 13, 2008 I'm thinking the issue revolves around contractions and was wondering if there were a finite number of generally used instances, but even I make 'em up as I go... http://www.enchantedlearning.com/grammar/contractions/list.shtml http://homeschool.consumerhelpweb.com/subjects/languagearts/commoncontractionssimple.htm Quote Link to comment https://forums.phpfreaks.com/topic/136864-solved-extract-quoted-strings/#findComment-714832 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.