0

This regex is used to replace text links with a clickable anchor tag.

#(?<!href="|">)((?:https?|ftp|nntp)://[^\s<>()]+)#i

My problem is, I don't want it to change links that are in things like <iframe src="http//... or <embed src="http://...

I tried checking for a whitespace character before it by adding \s, but that didn't work.

Or - it appears they're first checking that an href=" doesn't already exist (?) - maybe I can check for the other things too?

Any thoughts / explanations how I would do this is greatly appreciated. Main, I just need the regex - I can implement in CakePHP myself.

The actual code comes from CakePHP's Text->autoLink():

function autoLinkUrls($text, $htmlOptions = array()) {
    $options = var_export($htmlOptions, true);
    $text = preg_replace_callback('#(?<!href="|">)((?:https?|ftp|nntp)://[^\s<>()]+)#i', create_function('$matches',
        '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], $matches[0],' . $options . ');'), $text);
    return preg_replace_callback('#(?<!href="|">)(?<!http://|https://|ftp://|nntp://)(www\.[^\n\%\ <]+[^<\n\%\,\.\ <])(?<!\))#i',
        create_function('$matches', '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "http://" . $matches[0],' . $options . ');'), $text);
}

1 Answer 1

2

You can expand the lookbehind at the beginning of those regexes to check for src=" as well as href=", like this:

(?<!href="|src="|">)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.