Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php preg_match可搜索字符串中的数字_Php - Fatal编程技术网

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)转义斜杠,否则它将被视为正则表达式的分隔符