php 正则表达式 截取字符

来源:百度知道 编辑:UC知道 时间:2024/04/30 08:07:23
想要提取出sort-后面的.html前面的数字 例如:
$s="http://localhost:8080/emlog_3.3.0/src/sort-6.html";
只需要提取出6来就好了 可是我这样写的不对
<?php
$s="http://localhost:8080/emlog_3.3.0/src/sort-6.html";
preg_match('/-(.*?)./',$s,$matched);
echo($matched[1]);
?>
谢谢!

/-(.*?)\./

.需要被\掉才行啊

<?php

$s="http://localhost:8080/emlog-3.3.0/src/sort-6.html";

$str = preg_match('/-(\d*?)\./i' , basename($s) , $matchs1);

$str = preg_match('/-(\d*?)\./i' , $s, $matchs2);

echo '<li>' , $matchs1[1];

echo '<li>' , $matchs2[1];

echo '<li>' , get_str($s);

echo '<li>' , get_str('d');

error_reporting(E_ALL);

function get_str($s , $flag_1 = '-' , $flag_2 = '.'){

$pos_1 = strrpos($s , $flag_1) === false ? 0 : strrpos($s , $flag_1) + 1;

$len = strrpos($s , $flag_2) === false ? 0 : strrpos($s , $flag_2) - $pos_1;

return substr($s , $pos_1 , $len);
}

?>

这个:/(?: