BizLab Posted April 15, 2011 Share Posted April 15, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/233841-pseudo-tag-autolink-regex-tweak-needed/ Share on other sites More sharing options...
BizLab Posted May 19, 2011 Author Share Posted May 19, 2011 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?? Quote Link to comment https://forums.phpfreaks.com/topic/233841-pseudo-tag-autolink-regex-tweak-needed/#findComment-1217773 Share on other sites More sharing options...
.josh Posted May 20, 2011 Share Posted May 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233841-pseudo-tag-autolink-regex-tweak-needed/#findComment-1218087 Share on other sites More sharing options...
BizLab Posted May 21, 2011 Author Share Posted May 21, 2011 Dude. Awesomeness is the KEY to REGEX i didn't realize the exclusion on the front end of this expression - simply perfect! Thanks a million once again CV! SOLVED! Quote Link to comment https://forums.phpfreaks.com/topic/233841-pseudo-tag-autolink-regex-tweak-needed/#findComment-1218355 Share on other sites More sharing options...
BizLab Posted May 22, 2011 Author Share Posted May 22, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/233841-pseudo-tag-autolink-regex-tweak-needed/#findComment-1218699 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.