drh19790711 2010-04-28 21:33
浏览 58
已采纳

正则表达式获取当前页面或目录名称?

I am trying to get the page or last directory name from a url

for example if the url is: http://www.example.com/dir/ i want it to return dir or if the passed url is http://www.example.com/page.php I want it to return page Notice I do not want the trailing slash or file extension.

I tried this:

$regex = "/.*\.(com|gov|org|net|mil|edu)/([a-z_\-]+).*/i";

$name = strtolower(preg_replace($regex,"$2",$url));

I ran this regex in PHP and it returned nothing. (however I tested the same regex in ActionScript and it worked!)

So what am I doing wrong here, how do I get what I want?

Thanks!!!

  • 写回答

8条回答 默认 最新

  • duanlingzei0170 2010-04-28 21:36
    关注

    You may try tho escape the "/" in the middle. That simply closes your regex. So this may work:

    $regex = "/.*\.(com|gov|org|net|mil|edu)\/([a-z_\-]+).*/i";
    

    You may also make the regex somewhat more general, but that's another problem.

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

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失