Can Regex make my code more efficient?

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Locust
Forum Commoner
Posts: 31
Joined: Sat Jul 22, 2006 10:26 am

Can Regex make my code more efficient?

Post by Locust »

Code: Select all

$smessage = ereg_replace("\"", """, $_POST['field']);
$smessage = ereg_replace("<", "<", $smessage);
$smessage = ereg_replace(">", ">", $smessage);
$smessage = ereg_replace("'", "'", $smessage);
$smessage = ereg_replace("&", "&", $smessage);
Basically getting characters that could possibly tamper with HTML to be replaced with HTML-friendly characters. Should I go about this a different way?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

htmlentities()
Locust
Forum Commoner
Posts: 31
Joined: Sat Jul 22, 2006 10:26 am

Post by Locust »

Didn't know about that one :oops:

Thanks
Locust
Forum Commoner
Posts: 31
Joined: Sat Jul 22, 2006 10:26 am

Post by Locust »

I also had another line I didn't add

Code: Select all

$smessage = ereg_replace(13, "<br>", $smessage);
To replace carriage returns. htmlentities() does not compensate for this. Should I use the code I wrote?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ereg is slow and soon to be deprecated.

htmlentities() doesn't care about carriage returns, they aren't entities.

nl2br()
Locust
Forum Commoner
Posts: 31
Joined: Sat Jul 22, 2006 10:26 am

Post by Locust »

feyd wrote:nl2br()
Perfect ;)
Post Reply