PHP preg_replace 给定的字符串路径
PHP preg_replace string path given
我对 PHP 中的 preg_replace 函数有疑问。我想不出一个模式和一个替代品。
我有这两个字符串和一些代码:
$dirname1 = 'hdadas/dasdad/dasd/period_1min';
$dirname2 = 'hdadas/dasdad/dasd/period_1min/abcdrfg.php';
$pieces1 = explode('/', $dirname1);
$pieces2 = explode('/', $dirname2);
$dirname1 = end($pieces1); // output will be period_1min
$dirname2 = end($pieces2); // output will be abcdrfg.php
$output = preg_replace($pattern, $replacement, $dirname1); // or (..,..,$dirname2
echo $output; // i need 1min(without period_) or abcdrfg (without .php)
更新:
function Cat($dirname)
{
$name = explode('/', $dirname);
$pattern = ???;
$replacement = ???;
return preg_replace($pattern, $replacement, $dirname1);
}
print(Cat('hdadas/dasdad/dasd/period_1min'))); // output need 1min only
print(Cat('hdadas/dasdad/dasd/period_1min/abcdrfg.php'))); // output need abcdrfg only
这应该适合你:
(这里我用basename()
to get only the last part of the path, then I use pathinfo()
to get the last part without the extension. After this I just use preg_replace()
把下划线前的都换成空串)
<?php
$dirname1 = "hdadas/dasdad/dasd/period_1min";
$dirname2 = "hdadas/dasdad/dasd/period_1min/abcdrfg.php";
$dirname1 = pathinfo(basename($dirname1))["filename"];
$dirname2 = pathinfo(basename($dirname2))["filename"];
echo $output = preg_replace("/(.*)_/", "", $dirname1);
?>
输出:
1min
abcdrfg
这个正则表达式如何 /^.+_|\.[^.]+$/
:
$dirname1 = 'hdadas/dasdad/dasd/period_1min';
$dirname2 = 'hdadas/dasdad/dasd/period_1min/abcdrfg.php';
$pieces1 = explode('/', $dirname1);
$pieces2 = explode('/', $dirname2);
$dirname1 = end($pieces1); // output will be period_1min
$dirname2 = end($pieces2); // output will be abcdrfg.php
$output = preg_replace('/^.+_|\.[^.]+$/', '', $dirname1); // or (..,..,$dirname2
echo $output,"\n"; // i need 1min(without period_) or abcdrfg (without .php)
$output = preg_replace('/^.+_|\.[^.]+$/', '', $dirname2); // or (..,..,$dirname2
echo $output,"\n"; // i need 1min(without period_) or abcdrfg (without .php)
输出:
1min
abcdrfg
我对 PHP 中的 preg_replace 函数有疑问。我想不出一个模式和一个替代品。
我有这两个字符串和一些代码:
$dirname1 = 'hdadas/dasdad/dasd/period_1min';
$dirname2 = 'hdadas/dasdad/dasd/period_1min/abcdrfg.php';
$pieces1 = explode('/', $dirname1);
$pieces2 = explode('/', $dirname2);
$dirname1 = end($pieces1); // output will be period_1min
$dirname2 = end($pieces2); // output will be abcdrfg.php
$output = preg_replace($pattern, $replacement, $dirname1); // or (..,..,$dirname2
echo $output; // i need 1min(without period_) or abcdrfg (without .php)
更新:
function Cat($dirname)
{
$name = explode('/', $dirname);
$pattern = ???;
$replacement = ???;
return preg_replace($pattern, $replacement, $dirname1);
}
print(Cat('hdadas/dasdad/dasd/period_1min'))); // output need 1min only
print(Cat('hdadas/dasdad/dasd/period_1min/abcdrfg.php'))); // output need abcdrfg only
这应该适合你:
(这里我用basename()
to get only the last part of the path, then I use pathinfo()
to get the last part without the extension. After this I just use preg_replace()
把下划线前的都换成空串)
<?php
$dirname1 = "hdadas/dasdad/dasd/period_1min";
$dirname2 = "hdadas/dasdad/dasd/period_1min/abcdrfg.php";
$dirname1 = pathinfo(basename($dirname1))["filename"];
$dirname2 = pathinfo(basename($dirname2))["filename"];
echo $output = preg_replace("/(.*)_/", "", $dirname1);
?>
输出:
1min
abcdrfg
这个正则表达式如何 /^.+_|\.[^.]+$/
:
$dirname1 = 'hdadas/dasdad/dasd/period_1min';
$dirname2 = 'hdadas/dasdad/dasd/period_1min/abcdrfg.php';
$pieces1 = explode('/', $dirname1);
$pieces2 = explode('/', $dirname2);
$dirname1 = end($pieces1); // output will be period_1min
$dirname2 = end($pieces2); // output will be abcdrfg.php
$output = preg_replace('/^.+_|\.[^.]+$/', '', $dirname1); // or (..,..,$dirname2
echo $output,"\n"; // i need 1min(without period_) or abcdrfg (without .php)
$output = preg_replace('/^.+_|\.[^.]+$/', '', $dirname2); // or (..,..,$dirname2
echo $output,"\n"; // i need 1min(without period_) or abcdrfg (without .php)
输出:
1min
abcdrfg