jeeva Posted May 28, 2008 Share Posted May 28, 2008 Hi frnds, I want to have a string as ( , ) separated.. For Example If i have a string like $str="(1 and 2) or 3" i have to split like array( 1->1 and 2, 2->3 ) if i have $str="1 and (2 and 3) and (4 or 5) or 6" i should have a array like array( 1->1, 2->2 and 3 3->4 or 5 4->6 ) Can i get like this? If anyone have done like this please let me know.That would be a great favor from u... Thanks Jeeva Quote Link to comment https://forums.phpfreaks.com/topic/107598-need-a-algorithm/ Share on other sites More sharing options...
abdus Posted May 28, 2008 Share Posted May 28, 2008 have you tried Regular Expressions ? Quote Link to comment https://forums.phpfreaks.com/topic/107598-need-a-algorithm/#findComment-551484 Share on other sites More sharing options...
jeeva Posted May 28, 2008 Author Share Posted May 28, 2008 No actually, I'm not that good to use Regular Expressions.. Could u plz tell me how? Quote Link to comment https://forums.phpfreaks.com/topic/107598-need-a-algorithm/#findComment-551486 Share on other sites More sharing options...
Rebelrebellious Posted May 28, 2008 Share Posted May 28, 2008 Some thoughts: trim the whitespace seperate by ' (' before the ( for each: find the position of the ')' = pos1 find the position of the last occurrance of AND or OR = pos2 if pos2 is < pos1 then the whole thing is one element ==> so add it to an array of your elements else rtrim the and or or trm the white space find the position of and or or = pos2 if pos2 < pos1 then the remaining string is one element ==> so add it to an array of your elements else we need to deal with a number seperate the string by the ')' after the ) then add the two parts to the array of your elements now that array is still populated with ANDs ORs (s and )s for each trim whitespace trim AND; trim OR; search for ( or ) - if none are found then each number is a seperate element replace ands and ors with commas then pull the elements out of the comma seperated list trim the whitespace off of these elements add the element to your final array if parenthesis were found trim ( and trim ) each remaining element has one of the following structures: N AND N AND N, N OR N OR N, [ N AND N OR N ] search for and count ANDs search for and count ORs If the last case is even possible -The last case is special - if both ands and ors exist then you should divide the string into two parts at the position of the OR and add both parts to this array that is being processed if it got this far, the element is all set for placement in your final array. Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/107598-need-a-algorithm/#findComment-551504 Share on other sites More sharing options...
abdus Posted May 28, 2008 Share Posted May 28, 2008 I hope this helps try change the string and see <?php $thestring='1 and (2 and 3) and (4 or 5) or 6'; $pattern1=array('/^\(/','/\)$/'); $takeoffbrakets=preg_replace($pattern1,'',$thestring); $pattern2='/(\) and \()|( and \()|(\) and )|(\) or \()|( or \()|(\) or )/'; $set=preg_replace($pattern2,'|',$takeoffbrakets); $splitted=explode('|',$set); print_r($splitted); ?> Quote Link to comment https://forums.phpfreaks.com/topic/107598-need-a-algorithm/#findComment-551506 Share on other sites More sharing options...
jeeva Posted May 28, 2008 Author Share Posted May 28, 2008 I have got a code that is very simple and good here that code <?php $str='1 and (2 and 3) and (4 or 5) or 6'; $tst=split("\)|\(",$str); print_r($tst); ?> Thanks frnds Jeeva Quote Link to comment https://forums.phpfreaks.com/topic/107598-need-a-algorithm/#findComment-551532 Share on other sites More sharing options...
sasa Posted May 28, 2008 Share Posted May 28, 2008 try <?php $str="1 and (2 and 3) and (4 or 5) or 6"; //$str="(1 and 2) or 3"; $b = preg_split('/(and|or)(?=[^)]+(\(|$))/i', $str); print_r($b); echo '<hr />'; foreach ($b as $k => $v){ $v = trim($v); $v = trim($v, '('); $v = trim($v, ')'); $b[$k] = $v; } print_r($b); ?> Quote Link to comment https://forums.phpfreaks.com/topic/107598-need-a-algorithm/#findComment-551922 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.