I've got a lovely function that automatically turns URLs & emails into the appropriate hrefs.
function HTMLEncode($text) {
return preg_replace("(\r\n|\n|\r)", "<br />", preg_replace(array("/(([\w\.]+))(@)([\w\.]+)\b/i", "/((ftp(7?):\/\/)|(ftp\.))([\w\.\/\&\=\?\-]+)/", "/((http(s?):\/\/)|(www\.))([\w\.\/\&\=\?\%\#\-]+)/"), array("<a href=\"mailto:$0\">$0</a>", "<a href=\"ftp$3://$4$5\" target=\"_blank\">ftp$3://$4$5</a>", "<a href=\"http$3://$4$5\" target=\"_blank\">http$3://$4$5</a>"), $text));
}
This is used on text read back from a database and presented to public site visitors. So for instance:
"Send email to bob@domain.com" (read back from the database) becomes "Send email to <a href="mailto:bob@domain.com">bob@domain.com</a>"
and similarly for urls starting with http or www.
Occasionally though I don't want this function to happen. When I want to present the email or url in a different context, I might input it thusly into the database:
"Send email to <a href="mailto:bob@domain.com">Bob Meador</a>"
or similarly
"Visit our <a href="http://www.domain.com">website</a>"
In this case I don't need the function to encode the href, but it goes right ahead and does it. I think what I want to do is disable the function if the string begins with "href", so I think I need a condition in my regular expression to match "NOT href" at the beginning.
I'm sure this is simple, but I'm finding it tricky. Any thoughts?