the_butcher Posted August 23, 2006 Share Posted August 23, 2006 ok, have a text file, with contents in format:"etc etc","etc etc""etc2","etc 22""12312","etc.. etc.."Now, i had a regular expression which seems to work except for in PHP. that is;/^"([^"]+)","([^"]+)"$/The line start and end are required. Quote Link to comment Share on other sites More sharing options...
effigy Posted August 23, 2006 Share Posted August 23, 2006 The [tt]/m[/tt] modifier puts the pattern in multi-line mode, which allows ^ to match after a new line, and $ before. Modifiers are placed after the last delimiter, e.g.: [tt]/pattern/m[/tt].A more extensible, and arguably cleaner solution follows:[code]<pre><?php $tests = array( '"etc etc","etc etc"', '"etc2","etc 22"', '"12312","etc.. etc.."' ); foreach ($tests as $test) { $array = preg_split('/[",]/', $test, -1, PREG_SPLIT_NO_EMPTY); print_r($array); }?></pre>[/code] Quote Link to comment Share on other sites More sharing options...
the_butcher Posted August 23, 2006 Author Share Posted August 23, 2006 i have tried /m, no luck. unfortunately i need to do this with just a regex, so your 2nd approach is neither valid. Quote Link to comment Share on other sites More sharing options...
rea|and Posted August 24, 2006 Share Posted August 24, 2006 Try to modify your regex in this way, as said you have to use the "m" modifier, plus I've added an optional rule for windows couse It uses \r\n instead of \n.[CODE]/^"([^"]+)","([^"]+)"\r?$/m[/CODE] 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.