Search code examples
phppreg-grep

How to use preg_grep to find urls


In my project i have mutiple .csv files with domain names like 1www.abc.com.csv, 2www.abc.com.csv,or 1m.xyz.com.au.csv, 2m.blackburneinvest.com.au.csv , 3m.blackburneinvest.com.au.csv

I want to search the pattern *.csv(excluding number) in an array with preg_grep.

I tried this so far

<?php
echo "<pre>";
$files = glob("*.csv");//gets list of all the .csv file names in dir
foreach ($files as $key => $value) 
{
    $pattern = preg_split("/(\d+)/", $value);//Spilts Number from rest file name
    $fl_array = preg_grep($pattern[1], $files);
}
print_r($fl_array);
?>

But with this i am getting an error saying

Warning:  preg_grep(): Delimiter must not be alphanumeric or backslash

How can i edit ? Thanks


Solution

  • Use this:

    preg_grep(sprintf('/%s/', preg_quote($pattern[1])), $files);