我需要一个php函数,它将删除并返回感叹号中对excel文件的任何引用。所以如果我有:
$string = "Blah blah blah, this is a paragraph 'n'n
and some more copy. 'n'n
!/uploads/excel_doc.xls! 'n'n
Blah blah. The end."
我想要一个这样的功能:
function excel_file($string){
$string = _[_reg ex filter__]___;
return $string;
}
返回的$string是:
array($original_string, $excel_file_name);
// original string should replace the excel file reference with the string "[excel]"
怎么样
$string = "Blah blah blah, this is a paragraph 'n'n
and some more copy. 'n'n
!/uploads/excel_doc.xls! 'n'n
Blah blah. The end.";
function excel_file($string){
preg_match('/!([^!]+)!/m', $string, $m);
$string = preg_replace('/!([^!]+)!/m', '[excel]', $string);
return array($string, $m[1]);
}
$arr = excel_file($string);
print_r($arr);
输出:
Array
(
[0] => Blah blah blah, this is a paragraph
and some more copy.
[excel]
Blah blah. The end.
[1] => /uploads/excel_doc.xls
)
如果您想删除!!
之间的任何内容,请尝试:
// Remove any string made of alphanumerics and "_-./" between !!
$string = preg_replace('/![a-z0-9_''-''.''/]+!/i', '', $string);
如果你只想在xls文件路径上这样做,请尝试这种变体:
// Remove any string made of alphanumerics and "_-./" that ends with .xml between !!
$string = preg_replace('/![a-z0-9_''-''.''/]+''.xls!/i', '', $string);
我找不到你说要用其他东西替换的部分(你不是说要删除吗?!?)。如果你能更准确一点,我很乐意进一步帮助你。
尝试以下表达式:带有preg_match 的/!((?:''/[''w''.''-]+)+)!/
这里有一个我用来帮助编写正则表达式的网站,我曾经用它来获得这个表达式-http://txt2re.com/index-php.php3?s=/上传/excel_doc.xls&-29&-28&1
它也可能对你有所帮助。总是仔细检查他们生成的表达式上的括号,他们总是提取比你需要的更多的数据。其中还包括一些php代码,如果这些代码有帮助的话。