gpt4 book ai didi

php - 仅从 URL 中检索类别 ID

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:41:55 25 4
gpt4 key购买 nike

我的 URL 结构为 www.site.com/listings/232-real-estate,其中 232 是类别 ID,real-estate 作为我的类别名称。

我只想检索数字 ID,用于使用类别 ID 从数据库中提取数据。我该怎么做?

最佳答案

您可以使用像这样简单的东西:

<?php
preg_match_all('#(\d+)#', $url, $matches);
$catid = $matches[0];
?>

但这并没有进行足够的检查。理想情况下,您希望基于/:

<?php

$elements = explode('/',$url);
$category = $elements[2];
$subelements = explode('-',$category);
$categoryid = intval($subelements[0]);

?>

关于php - 仅从 URL 中检索类别 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9859328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com