Regex...yes i know...please at least look once :)

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
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Regex...yes i know...please at least look once :)

Post by mikusan »

Well i have a piece of code... and my quesition is more of a please help me to improve it as well as it doesn't work!! :lol:

Well it work mostly but i think i have used too much code to do a simple thing...less chat here is the code:

Code: Select all

if( eregi( "^[a-z0-9]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*.[a-z]{2,3}$", $EditedMessage ) )
	{
		$EditedMessage = ereg_replace( "^[a-z0-9]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*.[a-z]{2,3}$", "<a href="mailto:\\0">\\0</a>", $EditedMessage );
	}
	else
	{
		$EditedMessage = ereg_replace("(([^\.<>[]]+\.)|([[]]+://))+". "[^\.<>[]]+\.[^<>[]]+", "<a href="http://\\0" target="_blank">\\0</a>", $EditedMessage);
	}
If you haven't noticed the first reg ex is to theoretically replace emails with the equivalent link to an email...well to make it short that doesn;t work...
the second replaces all types of links to the HTML equivalent ant that works fine...

Do you see a flaw in my logic or statement...is there a better way to do this? Also any ideas why my email recognizer sucks??

Thanks!!
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Regex fns (preg_match, preg_replace etc) are apparently slightly faster than posix (ereg).

Try this handy tool for tuning up your regex expressions:

http://www.weitz.de/regex-coach/#install
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

someone else was saying it the opposite way when i was asking about regex earlier


but perl has an advantage over posix.... the ability to do non-greedy

also, go to the local bookstore, bring a camera phone (so you don't have to remember or buy the book) and look at the e-mail stuff in orielly's php cook book ;)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

McGruff wrote:Regex fns (preg_match, preg_replace etc) are apparently slightly faster than posix (ereg).

Try this handy tool for tuning up your regex expressions:

http://www.weitz.de/regex-coach/#install
That application was indeed useful. Thought I'd bump the post. Thank you.
Post Reply