michaellunsford Posted January 4, 2008 Share Posted January 4, 2008 So I have a long string that I need to break into an array. One nasty field has spaces in it. I need to capture the single spaces, but stop at the first occurrence of a double-space. ([A-Z0-9]+)[\s]+([^\s{2}]+) just doesn't do it. Here's an example of the input: 634RA32 SOME STRING WITH SPACES B 250 0 N Any ideas? Link to comment https://forums.phpfreaks.com/topic/84527-solved-include-spaces-but-not-double-spaces/ Share on other sites More sharing options...
effigy Posted January 4, 2008 Share Posted January 4, 2008 <pre> <?php $str = '634RA32 SOME STRING WITH SPACES B 250 0 N'; print_r(preg_split('/\s{2,}/', $str)); ?> </pre> Link to comment https://forums.phpfreaks.com/topic/84527-solved-include-spaces-but-not-double-spaces/#findComment-430641 Share on other sites More sharing options...
michaellunsford Posted January 4, 2008 Author Share Posted January 4, 2008 ahhhhhh preg_split. I forgot about preg_split! Link to comment https://forums.phpfreaks.com/topic/84527-solved-include-spaces-but-not-double-spaces/#findComment-430644 Share on other sites More sharing options...
effigy Posted January 4, 2008 Share Posted January 4, 2008 The non-split solution: <pre> <?php $str = '634RA32 SOME STRING WITH SPACES B 250 0 N'; preg_match_all('/\b(??!\s{2,}).)+/', $str, $matches); print_r($matches); ?> </pre> Link to comment https://forums.phpfreaks.com/topic/84527-solved-include-spaces-but-not-double-spaces/#findComment-430650 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.