I want write a simple code that convert special words to special link (for wiki plugin), if it's not a link!
For example suppose we have a text "Hello! How are you?!"
and
we want convert are
to <a href="url">are</a>
, but if we have <a href="#"> Hello! How are you</a>?!
or Hello! <a href="url">How are you?!</a>
does not change. Because it's a link.
How can I do it in PHP?! With preg_replace
?! How to?
Thanks.
this code is about : if there is a some URL in some phrase it will convert to a link
To better clarify the issue:
I have a HTML code that have some tags. I want some words in that, converted to some links. But if it is a another link does not convert. See below advanced example for special word
you
that we want linked to the google:This <a href="#">is a sample</a> text. Hello?! How are you?! <a href="#1">Are you ready</a>?!
should be convert to:
This <a href="#">is a sample</a> text. Hello?! How are <a href="http://www.google.com">you</a>?! <a href="#1">Are you ready</a> ?!
Note that the first
you
changed, but that secondyou
was not changed, because it's in the another<a>
tag.Answer:
Because of this work has issue with regular expression, this problem can solve without regular expression. Here a simple solution is given:
It's easy.
Output:
First function output:
Hello! How <a href="#">are</a> you?!
Second function output:
Hello! how <a href="http://google.com">are</a> you?!
Alternative:
If you want to not detect
<a>
tags which were closed, you can use this alternative code:This gives the output:
Hello! how <a href="#"><a href="http://google.com">are</a> you?!