regex - php getting part of a URL into a string -
hi guys m using code id on url
$string = $url; $matches = array(); preg_match_all('/.*?\/(\d+)\/?/s', $string, $matches); $id = $matches[1][0];
this code works urls like
http://mysite.com/page/1 http://mysite.com/page/somepage/2 http://mysite.com/page/3/?pag=1
i have id = 1 / id = 2 / id = 3
but url this
http://mysite.com/page/122-page-name/1
this returns id = 122
the id m try last part of url or have /?p= after
so urls type can have
http://mysite.com/page/1 http://mysite.com/page/some-page/2 http://mysite.com/page/12-some-name/3 http://mysite.com/page/some-page/4/?p=1 http://mysite.com/page/13-some-page/5/?p=2
id = 1 / id = 2 / id = 3 / id = 4 / id = 5
if id located @ end of url, explode
contents of url , take last element of resulting array. if may include variables (like ?pag=1
) can add validation after explode
check variable.
$urlarray = explode('/', $url); $page = end($urlarray); if(strpos($page, 'pag')!==false){ //get contents of variable $page variable //exploding variable through ? variable , getting //the numeric characters @ end }
Comments
Post a Comment