Reputation: 1520
i have an array of urls
[
'http://www.example.com/eng-gb/products/test-1',
'http://www.example.com/eng-gb/products/test-3',
'http://www.example.com/eng-gb/about-us',
]
I need to write a regex for filter only the ones end with:
http://www.example.com/eng-gb/products/(.*)
in this case i need to exclude 'about-us'.
I need also use 'http://www.example.com/eng-gb/products/(.*)'
as regex.
The best way for archive?
Upvotes: 3
Views: 2108
Reputation: 47864
preg_grep()
provides a shorter line of code, but because the substring to be matched doesn't appear to have any variable characters in it, best practice would indicate strpos()
is better suited.
Code: (Demo)
$urls=[
'http://www.example.com/eng-gb/products/test-1',
'http://www.example.com/eng-gb/badproducts/test-2',
'http://www.example.com/eng-gb/products/test-3',
'http://www.example.com/eng-gb/badproducts/products/test-4',
'http://www.example.com/products/test-5',
'http://www.example.com/eng-gb/about-us',
];
var_export(preg_grep('~^http://www.example\.com/eng-gb/products/[^/]*$~',$urls));
echo "\n\n";
var_export(array_filter($urls,function($v){return strpos($v,'http://www.example.com/eng-gb/products/')===0;}));
Output:
array (
0 => 'http://www.example.com/eng-gb/products/test-1',
2 => 'http://www.example.com/eng-gb/products/test-3',
)
array (
0 => 'http://www.example.com/eng-gb/products/test-1',
2 => 'http://www.example.com/eng-gb/products/test-3',
)
Some notes:
Using preg_grep()
:
.com
./products/
but not /products
. This is in accordance with the details in your question.Using strpos()
:
strpos()===0
means that the substring must be found at the start of the string.Upvotes: 1
Reputation: 7220
You'll need to escape the forward slashes and periods to get http:\/\/www\.example\.com\/eng-gb\/products\/(.*)
. After that, you could just place the URL in directly.
Alternatively (better) would be to search for \/eng-gb\/products\/(.*)
.
Example:
$matches = array();
preg_match('/\/eng-gb\/products\/(.*)/', $your_url, $matches);
$product = $matches[1];
Upvotes: 0
Reputation: 468
I think you need use preg_grep cause you have array of urls and this will return array of url that match your condition
$matches = preg_grep('/products\/.*$/', $urls);
and also you can use validate filters in php to validate urls
Upvotes: 0