【问题标题】:PHP - How to get ID from URL [closed]PHP - 如何从 URL 获取 ID [关闭]
【发布时间】:2013-10-03 14:07:56
【问题描述】:

我有 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

我想从两个 url 上面获取 23180 主题 ID。

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

【问题讨论】:

  • 要求代码的问题必须证明对正在解决的问题有最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期的结果。

标签: php url explode


【解决方案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);
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-14
  • 1970-01-01
  • 2013-09-27
  • 1970-01-01
相关资源
最近更新 更多