dpnv33177 2013-08-06 18:32
浏览 25
已采纳

wordpress url使用php解析 - 解析为php变量

hello wordpress/ url/ php experts I really appreciate your help in advance.

I set up a wordpress website and the url for example

www.domain.com/hello-world-how-are-you

so what i want to do is within single.php script, i want to parse the url and explode the url by "-" and take "hello" and "you" as a php variable.

i don't have too much knowledge on the server configuration, it would be great if this can be done using php only.

once again, thank you in advance.

  • 写回答

2条回答 默认 最新

  • dongxu7408 2013-08-06 18:42
    关注

    To grab the "page name"

    <?php 
    // gets you the path after http://www.yourdomain.com
    $path = $_SERVER["REQUEST_URI"];
    
    $current_url = explode("-", $path);
    $current_url = $current_url[1];
    
    echo "path ".$path . "<br />" ;
    echo "exploded url " .$current_url; 
    
    ?>
    

    I was able to make this method work with preg_replace to grab arrays of each word (stripping out everything but numbers and letters in a string.

    <?php 
    // gets /beach/coast-guard-beach/
    $path = $_SERVER["REQUEST_URI"];
    $preg = preg_split('/[^a-z0-9]/', $path);
    foreach ($preg  as $key) {
        print $key. "<br /> ";
    }
    ?>
    

    prints:

    beach coast guard beach

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?