Hi,
Is there a simple way to get all weird charaters out of a string?
i.e. $string = "'¼¶ƒ";
In other words how do you get rid of ALL non-alphanumeric characters from a string?
maybe a regular expression?
Thanks
joeblock wrote:Hi, Is there a simple way to get all weird charaters out of a string? i.e. $string = "'¼¶ƒ"; In other words how do you get rid of ALL non-alphanumeric characters from a string? maybe a regular expression? Thanks
<?php $string = "This is some text and numbers 12345 and symbols !£$%^&"; $new_string = ereg_replace("[^A-Za-z0-9]", "", $string); echo $new_string ?>
Hope this helps...
Hi Ian,
Thanks .. That worked great..
How would I modify that preg_replace so that I could keep certain characters..
i.e. $string = "http://www.example.com/@hello_there¼¶ƒ_hello the test";
I would like to keep the _:// and get rid of the @ TAB etc
I guess what I need is an url cleanser
Cheers
joeblock wrote:Hi Ian, Thanks .. That worked great.. How would I modify that preg_replace so that I could keep certain characters.. i.e. $string = "http://www.example.com/@hello_there¼¶ƒ_hello the test"; I would like to keep the _:// and get rid of the @ TAB etc I guess what I need is an url cleanser Cheers
Joe,
Try something like this:
<?php $new_string = ereg_replace("[^a-zA-Z0-9!:/.]", "", $string); ?>
Hope that helps.
I tried this <?php $string = "http://www.example.com/@hello_there¼¶ƒhello the test¬!-"; $new_string = ereg_replace("[A-Za-z/-9.-]", "", $string); ?> and it gave this http//www.example.com/hello_there_hellothetest-
Can you see anything wrong with this? I do not have the !
joeblock wrote:Hi Ian, I tried this <?php $string = "http://www.example.com/@hello_there¼¶ƒhello the test¬!-"; $new_string = ereg_replace("[A-Za-z/-9.-]", "", $string); ?> and it gave this http//www.example.com/hello_there_hellothetest- Can you see anything wrong with this? I do not have the ! Cheers
What are you trying to accomplish? Everything in the expression is an exception, so basically it will remove anything that is not in there.
Thanks for your help.
It works fine.