-1

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

Penny
  • 824
  • 1
  • 14
  • 31
  • 1
    Try something like this: `$fl_array = preg_grep("/" . $pattern[1] . "/", $files);` Does that do the trick for you? – Rizier123 Jan 07 '15 at 12:40

2 Answers2

2

Use this:

preg_grep(sprintf('/%s/', preg_quote($pattern[1])), $files);
Flosculus
  • 6,880
  • 3
  • 18
  • 42
0

String inside double quotes is being evaluated and "\d" after all became not you expect it is.

Either use single quotes:

preg_split('/(\d+)/' ...

or escape backslash in front of d:

preg_split("/(\\d+)/" ...
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160