ereg_replace and preg_replace question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

ereg_replace and preg_replace question

Post 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);
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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()
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Ok thanks feyd.
Post Reply