electricshoe Posted April 29, 2008 Share Posted April 29, 2008 I have a query and I want to parse the WHERE conditions to a human readable format. The conditions are already seperated and they look like this: make_id = 'Ford' AND model_id = 'Focus' AND year = '2005' And I want to parse it and make it look like this: Make: Ford Model: Focus Year: 2005 Now what I'm working with is a rather messy set of explodes and strpos that breaks way too easily. What I want to do is use regex to capture the innerpart of the quotes, is there an easy way to use regex to pull that part of the string out? It would be matching between the text and pulling out the value between the quotes. Any help is greatly appreciated, Thank you many moons in advance:) Quote Link to comment https://forums.phpfreaks.com/topic/103441-advanced-string-findreplaceregex-question/ Share on other sites More sharing options...
rhodesa Posted April 29, 2008 Share Posted April 29, 2008 <?php $human = array( 'make_id' => 'Make', 'model_id' => 'Model', 'year' => 'Year', ); $string = "make_id = 'Ford' AND model_id = 'Focus' AND year = '2005'"; if(preg_match_all('/(\w+)\s*=\s*\'(.+?)\'/',$string,$matches)){ foreach(array_keys($matches[0]) as $n) print "{$human[$matches[1][$n]]}: {$matches[2][$n]}\n"; }else print "Failed to match"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/103441-advanced-string-findreplaceregex-question/#findComment-529709 Share on other sites More sharing options...
sasa Posted April 29, 2008 Share Posted April 29, 2008 try <?php $where = "make_id = 'Ford' AND model_id = 'Focus' AND year ='2005'"; preg_match_all('/([^ ]*)[ ]*=[ ]*([^ ]*)/', $where, $a); foreach ($a[0] as $k => $v){ echo ucfirst(trim($a[1][$k],'_id')),': '. trim($a[2][$k], "'"), "<br />\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/103441-advanced-string-findreplaceregex-question/#findComment-529714 Share on other sites More sharing options...
electricshoe Posted April 29, 2008 Author Share Posted April 29, 2008 Thank you both! You're both beautiful people! Rhodesa's code works great since I have a lot more field names than just those 3 and I like being able to redo the text for them. But sasa's regex was useful in covering the other search conditions (Less Than, More Than, Between). preg_match_all() was the function I was missing and I thank you both for enlightening me to it. Now the fun part is coming up in which I have to interpret a rather long join query into human readable text as well, but this puts me well on my way. I'll post the basic result I end up with for further knowledge and then close this thread out. Thanks again for the help Quote Link to comment https://forums.phpfreaks.com/topic/103441-advanced-string-findreplaceregex-question/#findComment-529751 Share on other sites More sharing options...
electricshoe Posted April 29, 2008 Author Share Posted April 29, 2008 Modified code to handle between, less than, more than, and equals. This provides a summary of the query beautifully, although there are many more fields than just the 5 I have below. Thank you both again <?php $human = array( 'make_id' => 'Make', 'model_id' => 'Model', 'year' => 'Year', 'bodystyle' => 'Body Style', 'mileage' => 'Mileage' ); $string = $searchstring; if(preg_match_all('/(\w+)\s*=\s*\'(.+?)\'/',$string,$matches)) { foreach(array_keys($matches[0]) as $n) { print "{$human[$matches[1][$n]]}: {$matches[2][$n]}<br>"; } } if(preg_match_all('/([^ ]*)[ ]*>[ ]*([^ ]*)/',$string,$matches)) { foreach(array_keys($matches[0]) as $n) { print "{$human[$matches[1][$n]]}: More Than {$matches[2][$n]}<br>"; } } if(preg_match_all('/([^ ]*)[ ]*<[ ]*([^ ]*)/',$string,$matches)) { foreach(array_keys($matches[0]) as $n) { print "{$human[$matches[1][$n]]}: Less Than {$matches[2][$n]}<br>"; } } if(preg_match_all('/([^ ]*)[ ]*BETWEEN \']*([^ ]*)[\' AND \']*([^ ]*)[\']/',$string,$matches)) { foreach(array_keys($matches[0]) as $n) { print "{$human[$matches[1][$n]]}: ".number_format($matches[2][$n])."-".number_format($matches[3][$n])."<br>"; } } ?> Edit: How do you make the code colorful? Quote Link to comment https://forums.phpfreaks.com/topic/103441-advanced-string-findreplaceregex-question/#findComment-529777 Share on other sites More sharing options...
eRott Posted April 29, 2008 Share Posted April 29, 2008 Edit: How do you make the code colorful? Add the php tags. Example: <?php echo 'Hello dolly!'; ?> echo 'Hello dolly!'; Notice the difference. Quote Link to comment https://forums.phpfreaks.com/topic/103441-advanced-string-findreplaceregex-question/#findComment-529784 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.