网址字符串
http://localhost/magento_prac/index.php/electronics/computers.html
http://localhost/magento_prac/index.php/electronics/cell-phones.html
我想从 url 获取“计算机”。例如“电脑”、“手机”
最佳答案
尝试类似的事情
$category = preg_replace('/^.*\/(.*)\.html$/', '$1', $url);
正则表达式匹配从字符串开头 (^
) 到最后一个/(\/
) 的所有内容 (.*
) )。然后,它再次匹配除末尾的 .html
之外的所有内容 (.*
),并期望字符串在此之后结束 ($
) .
第二个 .*
周围的括号允许您将替换中的匹配部分引用为 $1
。
关于php - 如何从php中的url中获取单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35501980/