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 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址