Jump to content

Advanced String Find/Replace/Regex Question


electricshoe

Recommended Posts

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:)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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";
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
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.