-3

我有 2 个网址 -

$url="http://www.mysite.com/index.php?topic=23180.new#new";
$url="http://www.mysite.com/index.php/topic,23180.0.html";

在这两个 URL 上方,我没有在导航栏中检索它。这是从数据库表中检索并放入一个变量$url

我想23180从两个网址上方获取主题 ID。

现在如何获得这个主题ID?

4

1 回答 1

1

我应该尝试使用正则表达式...例如:

/**
 * 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);
}
于 2013-10-03T14:20:35.350 回答