在php中使用正则表达式从url中删除单词


removing a word from a url using regex in php

这是我的if语句,它将从源代码中获取这个例子:

href="/toronto/2013/05/14/the-airborne-toxic-event/4296/" 

从源代码

if ($text=preg_match('/href="([^"]*)"/', $text, $matches))
{
    $output=$matches[1];
    return $output;
}

并返回

/toronto/2013/05/14/the-airborne-toxic-event/4296/

我试图返回没有"/toronto""/toronto/"的url(我不确定哪一个我需要)

如果你能告诉我这个正则表达式,我会很感激的。由于

使用preg_replace:

return preg_replace('~^/?toronto/~', '', $output);

如果您确定"toronto/"不会出现在字符串的其他任何地方,您可以使用str_replace:

return str_replace('/toronto', '', $output);

这里假设总是有一个斜杠。