PHP-如何从URL获取ID
我有两个url-PHP-如何从URL获取ID,php,url,explode,Php,Url,Explode,我有两个url- $url="http://www.mysite.com/index.php?topic=23180.new#new"; $url="http://www.mysite.com/index.php/topic,23180.0.html"; 在这两个URL上方,我无法在导航栏中检索此URL。这是从数据库表中检索并放入一个变量$url 我想从以上两个url获取23180主题id 现在如何获取此主题id?我应该尝试使用正则表达式。。。比如: /** * Locate and ext
$url="http://www.mysite.com/index.php?topic=23180.new#new";
$url="http://www.mysite.com/index.php/topic,23180.0.html";
在这两个URL上方,我无法在导航栏中检索此URL。这是从数据库表中检索并放入一个变量$url
我想从以上两个url获取23180
主题id
现在如何获取此主题id?我应该尝试使用正则表达式。。。比如:
/**
* Locate and extract topic id from url received on this function.
*
* http://www.mysite.com/index.php?topic=23180.new#new
* http://www.mysite.com/index.php/topic,23180.0.html
*
* @param string Url that must be located.
* @return string Return that id located at string.
* @example
* <?php
* // must print 23180
* echo getTopicId("http://www.mysite.com/index.php?topic=23180.new#new");
* ?>
*/
function getTopicId($urlString)
{
return preg_replace('/topic(?:\=|\,)([0-9]+)\./i', '$1', $urlString);
}
/**
*从该函数接收的url中查找并提取主题id。
*
* http://www.mysite.com/index.php?topic=23180.new#new
* http://www.mysite.com/index.php/topic,23180.0.html
*
*必须找到的@param字符串Url。
*@return string返回位于字符串处的id。
*@example
*
*/
函数getTopicId($urlString)
{
返回preg\u replace('/topic(?:\=\\\,)([0-9]+)\./i'、'$1'、$urlString);
}
询问代码的问题必须表明对所解决问题的理解程度最低。包括尝试的解决方案,为什么它们不起作用,以及预期的结果。第二个是231800Hi@Carlos第一个是第二个是你有超过一半的路径,学习正则表达式,用base问你的问题。使用而不是