3

I have this line of code I use for SEO purposes. The only thing is that it has an ereg_replace function in it. Now I get "ereg_replace() is deprecated" error.

Apparently it's not as simple as switching it to preg_replace and my RegEx-fu is not too strong. Any help would be highly appreciated.

Thanks.

  //make it lowercase, remove punctuation, remove multiple/leading/ending spaces
    $return = trim(ereg_replace(' +',' ',preg_replace('/[^a-zA-Z0-9\s]/','',strtolower($input))));
0

2 Answers 2

3

Here you go.

$return = trim(preg_replace('/[ ]+/i',' ',preg_replace('/[^a-zA-Z0-9\s]/','',strtolower($input))));
4
  • This is the one that actually gives me the correct result. I wish it had more +1 to give. I actually up-voted the other answer first cause it was easier but once I ran tests this gave me the correct result. I can't seem to take away my vote from the other one though.
    – Serhiy
    Jul 14, 2011 at 21:32
  • 1
    @Serhiy Because I'm a regex badass. I saw his answer and I was like, "puhleaseeeeee, not even close" Jul 14, 2011 at 22:23
  • 1
    @Serhiy By the way, your domain "wellcommentedcode.com" is sexy name. Jul 14, 2011 at 22:28
  • haha thanks, I really need to do something with it... it's been a long overdue work in progress... your blog "eggheadreport.com" isn't bad either... could expand it to full-blown publication with a name like that...
    – Serhiy
    Jul 15, 2011 at 8:32
3

You just need to add delimeters

$return = trim(preg_replace('/ +/',' ',preg_replace('/[^a-zA-Z0-9\s]/','',strtolower($input))));

ereg_replace(' +' becomes preg_replace('/ +/'

Not the answer you're looking for? Browse other questions tagged or ask your own question.