web/asdfsadfas/asdfsdaf/badsfsadf/show_826.html
中间面包层数不定.
用正则怎么匹配呢
不想用explode
我想得到的最终的效果是
web/show_826.html
那要是web/和/show_826.html也不确定的话.能匹配得到这样web/show_826.html的结果吗?
<?php
$arr="web/asdfsadfas/asdfsdaf/badsfsadf/show_826.html
";
$arr=preg_replace("/(web\/)(.*)(show_826.html)/","\\1\\3",$arr);
echo $arr;
?>
运行结果:web/show_826.html
中间的东西都被替换掉了 就剩下了web/show_826.html了
试试这个
<?php
$arr="web/1/2/3/show_826.html
";
$arr=preg_replace("/^(.*\/)(.*?\/)(.*)$/U","\\1\\3",$arr);
echo $arr;
?>
$arr="web/asdfsadfas/asdfsdaf/badsfsadf/show_826.html
";
$arr=preg_replace("/(web\/)(.*)(show_826.html)/","\\1\\3",$arr);
echo $arr;
?>
运行结果:web/show_826.html
中间的东西都被替换掉了 就剩下了web/show_826.html了
试试这个
<?php
$arr="web/1/2/3/show_826.html
";
$arr=preg_replace("/^(.*\/)(.*?\/)(.*)$/U","\\1\\3",$arr);
echo $arr;
?>
温馨提示:答案为网友推荐,仅供参考
第1个回答 2010-10-14
<?php
$a = 'web/asdfsadfas/asdfsdaf/badsfsadf/show_826.html';
$bool = preg_match_all('/[A-Za-z0-9\/]*\/(\w+\.html)$/', $a, $matches);
print_r($matches);
?>
<?php
$a = 'web/asdfsadfas/asdfsdaf/badsfsadf/show_826.html';
$bool = preg_match_all('/([^\/]+)[A-Za-z0-9\/]*\/(\w+\.html)$/', $a, $matches);
print_r($matches);
?>
$a = 'web/asdfsadfas/asdfsdaf/badsfsadf/show_826.html';
$bool = preg_match_all('/[A-Za-z0-9\/]*\/(\w+\.html)$/', $a, $matches);
print_r($matches);
?>
<?php
$a = 'web/asdfsadfas/asdfsdaf/badsfsadf/show_826.html';
$bool = preg_match_all('/([^\/]+)[A-Za-z0-9\/]*\/(\w+\.html)$/', $a, $matches);
print_r($matches);
?>