Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/107598-need-a-algorithm/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/107598-need-a-algorithm/#findComment-551504
Share on other sites

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);
?>

Link to comment
https://forums.phpfreaks.com/topic/107598-need-a-algorithm/#findComment-551506
Share on other sites

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);
?>

Link to comment
https://forums.phpfreaks.com/topic/107598-need-a-algorithm/#findComment-551922
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.