在字符串中查找短语,并在短语后查找通配符


Look for phrase in string, and also look for wildcards after the phrase

我正在寻找一种根据网站URL显示一些代码的方法。

案例1:

如果网址包含getting-started执行某些操作

案例2:

如果 url 包含getting-started/*(可以是任何内容),则显示其他内容。

我目前得到的网址字符串为:

$url = $_SERVER['REQUEST_URI'];

然后执行strpos

if (strpos($slug,'getting-started')){

但是我需要一些东西来专门针对结尾getting-started的URL,并且还能够识别何时有更多内容。

你应该使用正则表达式:

(getting-started)$ // gets the phrase at the end of the string
(getting-started)(.*) // gets the phrase + anything else 

您可以将其与preg_match()结合起来