regex required to clean a string

hi

I have a string say $myString which can contain any case of number, letter, weird character and spacing

I need to do the following:

make it all lower case
replace spaces with -
remove everything that is not a-z 0-9 space . or -
replace multiple instances of - (caused by replacing spaces with -) to just -

So, something like "abcDEF %    %%:@~1  23.jPg" would become "abcdef-1-23.jpg"

full regex and php please
LVL 3
Neil ThompsonSenior Systems DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ddrudikCommented:
$string=strtolower($string);
$string=str_replace(' ','-',$string);
$string=preg_replace('/[^a-z0-9.-]/','',$string);
$string=preg_replace('/-+/','-',$string);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
emphamyCommented:
code attached =)

i spelt it out more there are shorter ways to do it but just wanted to show you the process

as far as removing whitespaces if there are too many spaces you can't really keep removing those without doing a loop, problem is if you do that you are just wasting tons of resources as loops can take a while depending on size of string.  

why not just remove whitespace all together?
<?php
 
$string = "abcDEF %    %%:@~1  23.jPg"
$string = strtolower($string);
$string = str_replace(" ","",$string);
$string = preg_replace('`[^a-z0-9]`i','',$string);
echo $string;
 
?>

Open in new window

0
emphamyCommented:
ddrudik beat me to it, as i was submitting i realized that i could have done +- .. good call.
0
Neil ThompsonSenior Systems DeveloperAuthor Commented:
Many thanks, worked instantly
0
ddrudikCommented:
Thanks for the question and the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.