Jump to content

preg_match_all wildcard for numbers


shoeshine

Recommended Posts

I hope someone here can help me soon with my issue

 

What I am trying to achieve here this:

 

I have an ajax form that has 3 dropdown menus on it representing a telephone number search.

 

dropdown 1 has values of

800, 877, 866, 8**  (8** means any of the 866, 877,800 number)

 

dropdown 2 is for the prefix of the number and can allow numbers and a *

ie:

55*

5**

***

 

dropdown 3 is for the suffix of the number and it's identical to dropdown 2, for the exception it will allow 4 numbers or * in the box.

 

Now when the ajax call goes out, it reads the the text file of numbers which are formatted as

8001234567 (no dashes) and I am storing those numbers into an array as $arg[]

 

So what I am asking is what would be the preg_match_all code to find these numbers being search for?

 

 

Thanks so much for your assistance!

 

 

 

Link to comment
Share on other sites

At least give it a try, the patterns should be pretty straight forward. There is a great tutorial on this site even that should help you out http://www.phpfreaks.com/tutorial/regular-expressions-part1---basic-syntax

 

3 hours later from reading this link a dozen time and I am still banging my head over this..I do not know  anymore than what I did when I started... Sorry. It just doesn't make sense.

 

Does anyone have some type of a working example I can go off of?

 

 

 

Link to comment
Share on other sites

Here's a way to do it:

 

<?php
$file = '1234567890
8005511234
7012344232
8995551934';
$search = '8**' . '55*' . '1*34';
//replace wildcards with regex equivalent
$search = str_replace('*', '[0-9]', $search);
preg_match_all("~^$search$~m", $file, $matches);
echo '<pre>' . print_r($matches[0], true) . '</pre>';
?>

 

Output:

Array
(
    [0] => 8005511234
    [1] => 8995551934
)

Link to comment
Share on other sites

Here's a way to do it:

 

<?php
$file = '1234567890
8005511234
7012344232
8995551934';
$search = '8**' . '55*' . '1*34';
//replace wildcards with regex equivalent
$search = str_replace('*', '[0-9]', $search);
preg_match_all("~^$search$~m", $file, $matches);
echo '<pre>' . print_r($matches[0], true) . '</pre>';
?>

 

Output:

Array
(
    [0] => 8005511234
    [1] => 8995551934
)

 

Thank you so much! You helped me out greatly. So much is appreciated!

 

 

 

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.