我需要一个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]"
答案 0 :(得分:0)
如果您要删除!!
之间的任何内容,请尝试:
// 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);
我无法得到你想要用其他东西替换它的部分(不要你说你要删除它?!?)。如果您可以更精确,我将很乐意为您提供进一步的帮助。
答案 1 :(得分:0)
尝试使用此表达式:/!((?:\\/[\\w\\.\\-]+)+)!/
和preg_match
这是我用来帮助编写正则表达式的网站,我曾用它来获取该表达式 - http://txt2re.com/index-php.php3?s=!/uploads/excel_doc.xls!&-29&-28&1
它也可能对你有所帮助。始终仔细检查其生成的表达式上的括号,它们总是提取超出您需要的数据。如果有任何帮助,也会包含一些PHP代码。
答案 2 :(得分:0)
怎么样
$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
)