BizLab Posted March 3, 2011 Share Posted March 3, 2011 Hey guys, long time. I'm using pseudo tags for markup within the documents of a wiki i am working on. While a simple string replace has worked on the rendering of most items, i find myself a little lost with the "[link][/link]" tag set because it includes html entities and i need quote literals. [*]I am cleaning strings with htmlentities() [*]I am using str_replace to switch each part of the link attribute, one at a time - BUT i would prefer a solution to convert an entire link tag (with href, including attributes - AND the actual text that goes between the two anchor tags - which already exists) from my - link /link - syntax to the standard <a>content</a> I need to: // convert this [link href="glossary.php?term=myterm"]My Term[/link] // TO this <a href="glossary.php?term=myterm">My Term</a> Any suggestions? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/229486-regex-to-find-all-quote-and-replace-with-quote-literal-only-when/ Share on other sites More sharing options...
.josh Posted March 5, 2011 Share Posted March 5, 2011 $string = "[link href="glossary.php?term=myterm"]My Term[/link]"; $string = html_entity_decode(preg_replace('~\[link([^\]]*)\](.*?)\[/link\]~i','<a$1>$2</a>',$string)); Quote Link to comment https://forums.phpfreaks.com/topic/229486-regex-to-find-all-quote-and-replace-with-quote-literal-only-when/#findComment-1183089 Share on other sites More sharing options...
BizLab Posted March 6, 2011 Author Share Posted March 6, 2011 $string = "[link href="glossary.php?term=myterm"]My Term[/link]"; $string = html_entity_decode(preg_replace('~\[link([^\]]*)\](.*?)\[/link\]~i','<a$1>$2</a>',$string)); Great progress! (my regex's are very very weak - obviously) The only thing that remains is to limit the html_entity_decode() to only the characters within the [link] tags because i don't want to convert every " in the entire text to " Sample case (non-sense text pulled from a random post) $haystack = 'This pattern is a slight modification in pattern "submitted" by Jacek Sompel. Using this tag one can also match anchor tags not having [link href="glossary.php?term=atrophic"]atrophic[/link] in href. This is useful for web crawler for crawling all links in a web page.'; Intended final output (html - i inserted newlines to prevent horizontal scrolling on the post) This pattern is a slight modification in pattern "submitted" by Jacek Sompel. Using this tag one can also match anchor tags not having <a href="glossary.php?term=atrophic">atrophic</a> in href. This is useful for web crawler for crawling all links in a web page. Sorry for not being more specific in the original post - and THANKS AGAIN for the help!! Quote Link to comment https://forums.phpfreaks.com/topic/229486-regex-to-find-all-quote-and-replace-with-quote-literal-only-when/#findComment-1183574 Share on other sites More sharing options...
.josh Posted March 6, 2011 Share Posted March 6, 2011 $string = "some random 'link' to [link href="glossary.php?term=myterm"]My Term[/link] blah "blah""; $string = preg_replace('~\[link([^\]]*)\](.*?)\[/link\]~ie','html_entity_decode(\'<a$1>$2</a>\')',$string); Quote Link to comment https://forums.phpfreaks.com/topic/229486-regex-to-find-all-quote-and-replace-with-quote-literal-only-when/#findComment-1183589 Share on other sites More sharing options...
BizLab Posted March 6, 2011 Author Share Posted March 6, 2011 Pure genius. HUGEEE Thanks, i will look this over and try to learn a thing or two Quote Link to comment https://forums.phpfreaks.com/topic/229486-regex-to-find-all-quote-and-replace-with-quote-literal-only-when/#findComment-1183616 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.