Jump to content

Help changing from ereg_replace to preg_replace


guestabc1

Recommended Posts

Hi I have come across a problem with my code since upgrading to php version 5.03 that ereg_replace can no longer be used, instead it is my understanding that I have to use preg_replace? If someone could point me in the right direction with the following code it would be hugely appreciated! Thanks in advance!

function format_strng($strng) {

$invalid_chars = '[^A-Za-z0-9+_.!*(),-]';	
$repeated_chars = '-{2,}';							

return strtolower(preg_replace($repeated_chars, '-', preg_replace($invalid_chars, '-', $strng)));
}

 

First, this should probably go in the PHP Regex section.

 

Second, you need to add delimiters to the regex. Like this:

 

$invalid_chars = '/[^A-Za-z0-9+_.!*(),-]/';   
$repeated_chars = '/-{2,}/';

 

Third, you probably also want to add a + or * to the character class in $invalid_char.

 

And finally, http://www.php.net/manual/en/reference.pcre.pattern.posix.php

 

Have fun.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.