dpnv33177 2013-08-06 10: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 10: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部