Page 1 of 1

ereg_replace and preg_replace question

Posted: Fri Feb 17, 2006 11:18 am
by Benjamin
Can someone tell me what this does?

Code: Select all

function sanitize_string($string) {
  $string = ereg_replace(' +', ' ', trim($string));
  return preg_replace("/[<>]/", '_', $string);
}

Posted: Fri Feb 17, 2006 12:43 pm
by feyd
The string is trimmed of surrounding whitespace. The ereg_ replaces multiple spaces with a single space. The preg_ replaces less-than and greater-than characters with underscores.

It's not very well written however. If anything, it should be using preg_ for both, but I'd only use regex for the spaces replacement. The latter is likely faster done with str_replace()

Posted: Fri Feb 17, 2006 12:47 pm
by Benjamin
Ok thanks feyd.