Jump to content

Pseudo tag Autolink Regex Tweak Needed


BizLab

Recommended Posts

Hey there,

 

I have a pseudo link tag [link][/link] and an auto hyperlink preg_replace. The auto hyperlink works just fine, but i want it to ignore any links that are inside my pseudo link element.

 

here is the hyperlink regex (which works great for any of you guys that need one - copy & paste BUT change the [link] to your standard <a> element)

 

$str = preg_replace('|(?<!href=")(https?://[A-Za-z0-9+\-=._/*(),@\'$:;&!?%]+)|i','[link href="$1" target="_blank"]$1[/link]', $str);
// Takes : http://www.mycoolsite.com/
// Outputs: [link href="http://www.mycoolsite.com/" target="_blank"]http://www.mycoolsite.com/[/link]  - WHICH IS GOOD, BUT read further

 

The problem comes when editing content that has already been hyperlinked. The output from that then becomes

 

// [link href="[link href="http://www.mycoolsite.com/" target="_blank"]http://www.mycoolsite.com/[/link]" target="_blank"]http://www.mycoolsite.com/[/link]

 

And so on. The only mod i need to make is to check for the [link] tag, and if it exists, do not re-hyperlink it.

 

Thanks to anyone with suggestions!

 

Link to comment
Share on other sites

  • 1 month later...

Anyone have ideas?? I'm very confident that the change is isolated to the first section, and will only need to identify the [link][/link] pseudo element instead of the standard <a></a> element. Everything i have tried has filed though..

 

$str = preg_replace('|(?<!href=") // changes here??
Link to comment
Share on other sites

Your posted regex can't make what you are saying it's making. 

 

You have: (?<!href=")(https?://[A-Za-z0-9+\-=._/*(),@\'$:;&!?%]+)

 

This pattern says to match (https?://[A-Za-z0-9+\-=._/*(),@\'$:;&!?%]+) if it is not preceded by href="

 

So if you have...

 

[link href="http://www.mycoolsite.com/" target="_blank"]http://www.mycoolsite.com/[/link]

 

...the http://www.mycoolsite.com/ in your [link] tag won't match, because it is preceded by href="

 

The only way you could be getting the output you say you are getting is if you are doing something more than this preg_replace(), and have not posted everything you are actually doing here. 

 

However, I do see a flaw in what you have posted:

 

test:

$str = "http://www.mycoolsite.com/";

$str = preg_replace('|(?<!href=")(https?://[A-Za-z0-9+\-=._/*(),@\'$:;&!?%]+)|i','[link href="$1" target="_blank"]$1[/link]', $str);
echo "round 1 : " . $str . "<br/>";

$str = preg_replace('|(?<!href=")(https?://[A-Za-z0-9+\-=._/*(),@\'$:;&!?%]+)|i','[link href="$1" target="_blank"]$1[/link]', $str);
echo "round 2 : " . $str . "<br/>";

$str = preg_replace('|(?<!href=")(https?://[A-Za-z0-9+\-=._/*(),@\'$:;&!?%]+)|i','[link href="$1" target="_blank"]$1[/link]', $str);
echo "round 3 : " . $str . "<br/>";

 

output:

round 1 : [link href="http://www.mycoolsite.com/" target="_blank"]http://www.mycoolsite.com/[/link]
round 2 : [link href="http://www.mycoolsite.com/" target="_blank"][link href="http://www.mycoolsite.com/" target="_blank"]http://www.mycoolsite.com/[/link][/link]
round 3 : [link href="http://www.mycoolsite.com/" target="_blank"][link href="http://www.mycoolsite.com/" target="_blank"][link href="http://www.mycoolsite.com/" target="_blank"]http://www.mycoolsite.com/[/link][/link][/link]

 

So I do see a flaw..but not what you are saying.  I see matching and replacing the original link inside the tag, not inside your [link..] tag. 

 

This should solve that...

$str = preg_replace('~(?<!link href="|\])(https?://[A-Za-z0-9+\-=._/*(),@\'$:;&!?%]+)~i','[link href="$1" target="_blank"]$1[/link]', $str);

 

p.s. - I changed your pattern delimiter from | to ~  you should not use | as the pattern delimiter because it has special meaning in regex (it is the "or" operator).  While it is possible to use | as the delimiter, it makes it very difficult to then use it as the "or" operator in the pattern.

 

 

Link to comment
Share on other sites

One more thing just to clarify the question of how my regex was behaving initially - it turns out that i was escaping the string before running this regex, so that once the string hit the regex for evaluation, the string didn't match, and was RE-converted

 

Thanks again CV

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.