explode() help

i need to explode a string (The string is dynamically)
“ID = 2 AND CITY IN (9) AND (YN = ‘N’ OR YN IS NULL) AND ((UPPER BETWEEN ‘300000’ AND ‘9900000000’) OR (LOWER BETWEEN ‘300000’ AND ‘9900000000’)) AND INCOME BETWEEN ‘50000’ AND ‘9900000000’ AND AGE BETWEEN ‘25’ AND ‘69’ AND PROFESSION_ID IN (1,2) AND ((TOTAL_EMI_AMT >=10000))”

the string needs to be xploded as

Array
(
    [0] =>PRODUCT_ID = 2 
    [1] =>CITY IN (9) 
    [2] =>(YN = 'N' OR YN IS NULL) 
    [3] =>((UPPER BETWEEN '300000' AND '9900000000') OR (LOWER BETWEEN '300000' AND    '9900000000')) 
    [4] =>INCOME BETWEEN '50000' AND '9900000000' 
    [5] =>AGE BETWEEN '25' AND '69' 
    [6] =>PROFESSION_ID IN (1,2) 
    [7] => ((TOTAL_EMI_AMT >=10000))
}

Instead when i use explode(‘AND’,$str) where $str is the above string i get the array as


Array
(
    [0] => ID = 2  
    [1] =>  CITY IN (9) 
    [2] =>  (YN = 'N' OR YN IS NULL) 
    [3] =>  ((UPPER BETWEEN '300000' 
    [4] =>  '9900000000') OR (LOWER BETWEEN '300000' 
    [5] =>     '9900000000')) 
    [6] =>  INCOME BETWEEN '50000' 
    [7] =>  '9900000000' 
    [8] =>  AGE BETWEEN '25' 
    [9] =>  '69' 
    [10] =>  PROFESSION_ID IN (1,2) 
    [11] =>  ((TOTAL_EMI_AMT >=10000))
)

PLZ HELP ON THIS…

I suspect that you are going to need some very custom programming and likely extensive use of regular expressions. I cannot see what you are asking being done with explode. As you have found out (no doubt) the delimiter goes away with the explode command so you would have to pull out the ((UPPER BETWEEN ‘300000’ AND ‘9900000000’) OR (LOWER BETWEEN ‘300000’ AND ‘9900000000’)) line first before you could explode it.

Or you could explode it and then look for the ((UPPER BETWEEN in the array elements and then loop until you find the next occurrence of )), either way things are going to get difficult, especially since you claim that the line “ID = 2 AND CITY IN (9) AND (YN = ‘N’ OR YN IS NULL) AND ((UPPER BETWEEN ‘300000’ AND ‘9900000000’) OR (LOWER BETWEEN ‘300000’ AND ‘9900000000’)) AND INCOME BETWEEN ‘50000’ AND ‘9900000000’ AND AGE BETWEEN ‘25’ AND ‘69’ AND PROFESSION_ID IN (1,2) AND ((TOTAL_EMI_AMT >=10000))” is created dynamically. Depending on HOW dynamic it is can make a HUGE difference in where you go with this one.

The easiest way I can see exploding this string (without the use of regex) is by first discerning which parts are bracketed. You can exclude those parts from being exploded on ‘AND’. As Peg110 said, you’re going to have to do some custom coding for that, and perhaps a regex would be faster and shorter code, but it would be a LOT harder to perfect or maintain.

Thnz to Zyppora n peg110 for your replies.
I did find a solution to the issue.
Even though the string that was generated was a dynamic one…i wz only facin problems with the AND in the BETWEEN clause
So i replaced the string ’ AND ’ with ’ and ’ and then exploded the string on basis of AND…this created the array as i wanted.

THNZ AGAIN :stuck_out_tongue: :D

Interesting solution… Probably wouldn’t have thought of that one.

Glad you could solve it relatively simply.

The reason you wouldn’t have thought of it, and neither would I, is because it’s a workaround, not a solution. I’m certainly hoping you have full control over how the WHERE clause is built up, because if there’s any external intervention in the AND vs. and decision, this could pose unexpected behaviour.

U may call it a workaround…and it definitely worked :wink: :slight_smile:

WOW… I Guess I am getting old or something… I try to keep up on the l33t, but that’s intense and cramps my brain to try and figure some of it out…

And I here I have been worried about proper grammar and punctuation! What was I thinking…

Haha, peg110.

:wink:
Sponsor our Newsletter | Privacy Policy | Terms of Service