fredroines Posted November 13, 2008 Share Posted November 13, 2008 I have a string like : $str= 'EM-SA0048-SIG_CODE-AA-ISTE-0033' so using preg_split , I would like to get : Array ( [0] => EM [1] =>SA0048 [2] => SIG_CODE [3]=>AA-ISTE [4]=>-0033 ) Please somebody can help me with this issue Thanks Quote Link to comment https://forums.phpfreaks.com/topic/132610-solved-regular-expression-in-php-using-preg_split/ Share on other sites More sharing options...
ddrudik Posted November 13, 2008 Share Posted November 13, 2008 Please define your rules for splitting the string, you appear to be splitting on - but not within AA-ISTE. if it is just splitting on "-" you could just use explode() instead. Quote Link to comment https://forums.phpfreaks.com/topic/132610-solved-regular-expression-in-php-using-preg_split/#findComment-689545 Share on other sites More sharing options...
fredroines Posted November 13, 2008 Author Share Posted November 13, 2008 Sort of , yes , It must be splitting on - , but the 3th part or element must not be ( [3]=>"XXXX-XXXX" ) and [4]=> 1234 (numeric part) Quote Link to comment https://forums.phpfreaks.com/topic/132610-solved-regular-expression-in-php-using-preg_split/#findComment-689570 Share on other sites More sharing options...
ddrudik Posted November 13, 2008 Share Posted November 13, 2008 Possibly: Raw Match Pattern: (?<!-[a-z]{2})-(?![a-z]{4}-) PHP Code Example: <?php $sourcestring="your source string"; $matches=preg_split('/(?<!-[a-z]{2})-(?![a-z]{4}-)/i',$sourcestring); echo "<pre>".print_r($matches,true); ?> $matches Array: ( [0] => EM [1] => SA0048 [2] => SIG_CODE [3] => AA-ISTE [4] => 0033 ) Depends on how it matches your data. Quote Link to comment https://forums.phpfreaks.com/topic/132610-solved-regular-expression-in-php-using-preg_split/#findComment-689586 Share on other sites More sharing options...
effigy Posted November 13, 2008 Share Posted November 13, 2008 Do you want to retain the last hyphen as your example shows? <pre> <?php $str = 'EM-SA0048-SIG_CODE-AA-ISTE-0033'; print_r(preg_split('/(?=-\d{4}$)|-(?!(?:.{4}-)?\d{4}$)/', $str)); ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/132610-solved-regular-expression-in-php-using-preg_split/#findComment-689590 Share on other sites More sharing options...
.josh Posted November 13, 2008 Share Posted November 13, 2008 If that's how your string is going to always look like, you could still just use explode and then glue elements 3 and 4 back together... Quote Link to comment https://forums.phpfreaks.com/topic/132610-solved-regular-expression-in-php-using-preg_split/#findComment-689619 Share on other sites More sharing options...
fredroines Posted November 13, 2008 Author Share Posted November 13, 2008 All of you are really good , It works ,but could it be more generic for [3] & [4] element, I mean let says that those elements are not a fix length example: EM-SA0048-SIG_CODE-AAXXX-IS-0033 or EM-SA0048-SIG_CODE-AAXX-ISTEXXX-0033 or EM-SA0048-SIG_CODE-AAXXXXXX-ISTEXXXXXXXXXXX-0033 Could be it possible? Quote Link to comment https://forums.phpfreaks.com/topic/132610-solved-regular-expression-in-php-using-preg_split/#findComment-689634 Share on other sites More sharing options...
ddrudik Posted November 13, 2008 Share Posted November 13, 2008 If that's how your string is going to always look like, you could still just use explode and then glue elements 3 and 4 back together... I like that idea better, no regex required. If the same number of parts exist as in your string example: <pre> <?php $str='EM-SA0048-SIG_CODE-AA-ISTE-0033'; $arr=explode('-',$str); $arr[3]=$arr[3].'-'.$arr[4]; $arr[4]=array_pop($arr); echo print_r($arr,true); ?> If the number of parts may vary different code will be required. Quote Link to comment https://forums.phpfreaks.com/topic/132610-solved-regular-expression-in-php-using-preg_split/#findComment-689644 Share on other sites More sharing options...
corbin Posted November 13, 2008 Share Posted November 13, 2008 Just change {4} to + if you want it to be 1 or more instead of 4. Quote Link to comment https://forums.phpfreaks.com/topic/132610-solved-regular-expression-in-php-using-preg_split/#findComment-689781 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.