I have string
$str = http://localhost/tim-yeu-cau/entry/XXX/?sort=5&dir=asc
I want to extract number XXX between entry/
and /
using preg_match
My code does not work.
preg_match('/^entry//', $str, $matches);
print_r($matches);
You can try this -
$str = 'http://localhost/tim-yeu-cau/entry/123/?sort=5&dir=asc';
preg_match('/entry\/(\d+)\//', $str, $m);
var_dump($m[1]);
Output
string(3) "123"
Try this
$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));
}