logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Coding & Scripting Corner View previous topic :: View next topic
Reply to topic   Topic: RegEx question
Author
Brian



Joined: 21 Oct 2005
Posts: 209
Location: Puyallup, WA USA

PostPosted: Fri 11 Dec '09 1:30    Post subject: RegEx question Reply with quote

Okay, so I have determined that for my purposes, using an "AJAX" type of search feature that it is very useful to modify the search phrase in this manner:
  • I type: AF 73 or AF73
  • It is evaluated as: A%F%7%3 for my SQL query.


As it turns out if I have a product with a db field containing FM73 (no space) and I type FM 73, no problem because I convert the space to the % wildcard character. But if the product in the DB is FM 73 (with space) and I type FM73 (no space), it is not found.

If I apply RegEx to change the evaluation to have a % between each character in the search phrase I get the matches I desire and none of the matches I don't want.

So my question is how to write the RegEx to convert "FM73" or "FM 73" or even "F M 7 3" for that matter to "F%M%7%3"

I hope this makes sense.

You can see what I mean at http://www.pccomposites.com where at the top is a search feature with the active search feature. This is what I want to improve.

Thanks.
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7288
Location: Germany, Next to Hamburg

PostPosted: Fri 11 Dec '09 12:27    Post subject: Reply with quote

You may try this

Code:

$string = 'AF73';
$len = strlen($string);
$new_string = "";
for($i=0; $i<$len; $i++){
  $new_string .= $string{$i}.'%';
}

//echo $new_string;
Back to top
Brian



Joined: 21 Oct 2005
Posts: 209
Location: Puyallup, WA USA

PostPosted: Fri 11 Dec '09 19:48    Post subject: Reply with quote

Okay, that works. I had not thought of such a simplified approach - I was seeing regular expressions as my best choice. This is very easy, thanks.

So I implemented this change and found it actually did in fact grab some items in the search that I did not want.

What I realize now is that I need to put a % deliminator not between each character but between letters and numbers. I am sorry for the confusion, this is my first real attempt at an AJAX style search.

So if I type: FM73
I want to convert that to: FM%73

If I type: BMS101
Convert to: BMS%101

The issue is that I have to work with the data I am given. I cannot change the database to suit a more optimal search therefore the search has to be suited for the data (does that make sense?). Some of the part numbers have a space between the letters and numbers, some do not have a space. How do I as the customer know?

That is why this is so tricky and challenging to me.

I did not foresee the original question being the wrong question to ask. And your response James would have been ideal.
Back to top
glsmith
Moderator


Joined: 16 Oct 2007
Posts: 2268
Location: Sun Diego, USA

PostPosted: Fri 11 Dec '09 21:07    Post subject: Reply with quote

Is there no PHP equivelent to

$in =~ s/\d/\%$&/o;

Code:
$in = "BMS101";
print "In: ".$in."\n";
$in =~ s/\d/\%$&/o;
print "Out: ".$in."\n";


E:\build\tmp>testregx
In: BMS101
Out: BMS%101
Back to top
glsmith
Moderator


Joined: 16 Oct 2007
Posts: 2268
Location: Sun Diego, USA

PostPosted: Fri 11 Dec '09 21:27    Post subject: Reply with quote

Yes, there is

Code:
$string = 'BMS101';
echo "In: ",$string, "\n";
echo "Out: ", preg_replace('/\d/', '%${0}', $string, 1), "\n";


E:\build\tmp>php testregx.php
In: BMS101
Out: BMS%101


for the wrong question
Code:
$string = 'BMS101';
$out = preg_replace('/\w/', '%${0}', $string);
$out = preg_replace('/%/', '', $out, 1);
echo "Out: ", $out, "\n";


Out: B%M%S%1%0%1


Last edited by glsmith on Fri 11 Dec '09 22:09; edited 2 times in total
Back to top
Brian



Joined: 21 Oct 2005
Posts: 209
Location: Puyallup, WA USA

PostPosted: Fri 11 Dec '09 21:50    Post subject: Reply with quote

That did it!

Can you briefly explain how that worked though? If you don't mind of course.

You clearly have more in depth knowledge of REGEX and such than I do.

Thank you.
Back to top
glsmith
Moderator


Joined: 16 Oct 2007
Posts: 2268
Location: Sun Diego, USA

PostPosted: Fri 11 Dec '09 21:57    Post subject: Reply with quote

Thanks to Perl.

Sure, maybe I can, I'll try;

The handiest tool is substitution using regx
substitution in perl 'removes" the target and stores it in $&
same in PHP just it's ${0}

We want to place it between the alpha and the decimal so we search for the first character after were we want to stick in the replacement (which happens to be a decimal \d ).
We replace with what we want and in your case, we need to put back in the 'removed' piece. So that is what we have done. Told it to find the first decimal and replace it with our % and put the first decimal back.

hence the '1' out at the end of the of the call to preg_replace tell it to match one time and one time only.

Notice how for the wrong question we have it seach for any word character /w (A-Za-z0-9) and do not tell it to stop after the first match. It will push a % before the first match (the B in this case) so we go right behind and strip that one off, again telling it to only replace once.

Help? Confuse?
Back to top


Reply to topic   Topic: RegEx question View previous topic :: View next topic
Post new topic   Forum Index -> Coding & Scripting Corner