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
https://forums.phpfreaks.com/topic/175388-preg_match_all-wildcard-for-numbers/
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?

 

 

 

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
)

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!

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.