简体   繁体   中英

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.cs v, 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

用这个:

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

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+)/" ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM