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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?