Php preg_match可搜索字符串中的数字
我有绳子Php preg_match可搜索字符串中的数字,php,Php,我有绳子 $str = http://localhost/tim-yeu-cau/entry/XXX/?sort=5&dir=asc 我想使用preg\u match提取entry/和/之间的数字XXX 我的代码不起作用 preg_match('/^entry//', $str, $matches); print_r($matches); 你可以试试这个- $str = 'http://localhost/tim-yeu-cau/entry/123/?sort=5&dir=a
$str = http://localhost/tim-yeu-cau/entry/XXX/?sort=5&dir=asc
我想使用preg\u match
提取entry/
和/
之间的数字XXX
我的代码不起作用
preg_match('/^entry//', $str, $matches);
print_r($matches);
你可以试试这个-
$str = 'http://localhost/tim-yeu-cau/entry/123/?sort=5&dir=asc';
preg_match('/entry\/(\d+)\//', $str, $m);
var_dump($m[1]);
输出
string(3) "123"
你可以试试这个-
$str = 'http://localhost/tim-yeu-cau/entry/123/?sort=5&dir=asc';
preg_match('/entry\/(\d+)\//', $str, $m);
var_dump($m[1]);
输出
string(3) "123"
试试这个
$str = "http://localhost/tim-yeu-cau/entry/1213213/?sort=5&dir=asc";
$from = "entry/";
$to = "/?sort";
echo getStringFrmStrtEnd($str,$from,$to);
function getStringFrmStrtEnd($str,$from,$to)
{
$sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
return substr($sub,0,strpos($sub,$to));
}
试试这个
$str = "http://localhost/tim-yeu-cau/entry/1213213/?sort=5&dir=asc";
$from = "entry/";
$to = "/?sort";
echo getStringFrmStrtEnd($str,$from,$to);
function getStringFrmStrtEnd($str,$from,$to)
{
$sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
return substr($sub,0,strpos($sub,$to));
}
1) 使用^
你说字符串必须以条目开始
2)使用捕获组和\d+
捕获斜杠之间的数字3)退出斜杠,否则它将被视为正则表达式的分隔符1)使用^
你说字符串必须以条目开始
2)使用捕获组和\d+
以捕获斜杠之间的数字3)转义斜杠,否则它将被视为正则表达式的分隔符