我有一个这样的URL:www.test.fr/dir/file.html#hello
我会删除字符#
之后的所有内容。我试过这个/#[a-z0-9]+/
你的代码几乎是正确的,这工作得很好:
$new = preg_replace('/#[a-z0-9]+/', '', 'www.test.fr/dir/file.html#hello');
print ($new);
prints:
www.test.fr/dir/file.html
你可以在这里测试
你可以用"#"爆炸并获得第一个位置。像这样:
$url = "www.test.fr/dir/file.html#hello";
$result = explode("#",$url)[0];