Jump to content

Regex to Find all &quote and replace with quote literal ONLY WHEN..


BizLab

Recommended Posts

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!

Link to comment
Share on other sites

$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!!

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.