simon
simon

Reputation: 1234

Preg replace php delete after character

I have a URL like this : www.test.fr/dir/file.html#hello I would delete everything after the character #. I have try this /#[a-z0-9]+/

Upvotes: 2

Views: 84

Answers (2)

Ilanus
Ilanus

Reputation: 6928

You code is almost correct, this works just fine:

$new = preg_replace('/#[a-z0-9]+/', '', 'www.test.fr/dir/file.html#hello');
print ($new);

prints:
www.test.fr/dir/file.html

You can test it here

Upvotes: 2

jolmos
jolmos

Reputation: 1575

You can explode by '#' and get the first position. Something like this:

$url = "www.test.fr/dir/file.html#hello";
$result = explode("#",$url)[0];

Upvotes: 1

Related Questions