Hey,

I have this line of code... because I dont want to allow things like & or ! or * etc... but I DO want to allow _ and -

$url = ereg_replace("[^A-Za-z0-9 ]", "", $url);

Can anyone help me?

Dan

Recommended Answers

All 6 Replies

ereg_replace was deprecated in PHP 5.3 so usage of it is not recommended.

You can however use a similar function preg_replace

$url = preg_replace("[^A-Za-z0-9_- ]", "", $url);

This will allow any letters or numbers, underscores, hyphens or spaces.

Hope this helps.

Thanks!, but this doesnt seem to work at all =/

Dan

Karma for not testing... THis works

$url = preg_replace("/[^A-Za-z0-9_\- ]/", "", $url);

Works like a charm, Thanks!

Dan,
what exactly are you trying to do?...
you could try

$url = "http://someurl.com/some****&!!!!!other_text";
$url = str_replace("*", "", $url);
$url = str_replace("&", "", $url);
$url = str_replace("!", "", $url);
// or str_replace("whatever you want removed","replacement value",string_to_search);
echo "url = " . $url . "<br>";

No problem, glad I could be of service.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.