View previous topic :: View next topic |
Author |
Message |
primozs Smarty Rookie
Joined: 14 Jan 2017 Posts: 8
|
Posted: Thu Jan 19, 2017 3:50 pm Post subject: Creating hyperlinks in the text |
|
|
I wold like to change all URLs in a text to hyperlinks, e.g. http://www.google.com would change to Code: | <a href="http://www.google.com">http://www.google.com</a> |
I have checked the other thread talking about similar issue http://www.smarty.net/forums/viewtopic.php?t=5762&highlight=hyperlink and I wanted to use that code. So I am trying to modify the $string variable that contains the text like this (OK, i replaced ereg_replace with newer preg_replace):
Code: | {$string = preg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\" TARGET=\"_blank\">\\0</a>", $string)} |
Unfortunately it doesn't work. Instead of modifying the $string variable it prints ou on the WEB page "http://www.google.com".
Any suggestion? |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Thu Jan 19, 2017 4:51 pm Post subject: |
|
|
That other thread was creating a function that uses regular PHP code in it.
Your approach is wanting to use Smarty syntax within the template. So, based off your example...
Code: | {assign var="string" value=preg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\" TARGET=\"_blank\">\\0</a>", $string)} |
There is also Smarty's own variable modifier regex_replace.
In your pattern expression, I don't see any capturing parentheses. Also, preg_replace needs a unique character delimiter. A | (pipe) or # (hash) should work for this.
Code: | preg_replace("#([[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/])#","<a href=\"\\0\" TARGET=\"_blank\">\\0</a>", $string) |
I assume you have managed to differentiate between what looks like a stand-alone URL versus a URL already in an anchor tag. |
|
Back to top |
|
primozs Smarty Rookie
Joined: 14 Jan 2017 Posts: 8
|
Posted: Fri Jan 20, 2017 6:45 am Post subject: |
|
|
Thank you for very fast answer.
I have tried with the "assign" already and hasn't worked, but it looks like correct approach. The results from the suggestions are bellow.
1.
Code: | {assign var="string" value=preg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\" TARGET=\"_blank\">\\0</a>", $string)} |
The WEB page looks like:
Quote: | preg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","\\0", |
The HTML code is:
Code: | <p>preg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href="\"\\0\"" target="\"_blank\"">\\0</a>",</p> |
2.
Code: | {assign var="string" value=preg_replace("#([[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/])#","<a href=\"\\0\" TARGET=\"_blank\">\\0</a>", $string)} |
The WEB page looks like:
Quote: | preg_replace("#([[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/])#","\\0", |
The HTML code is:
Code: | <p>preg_replace("#([[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/])#","<a href="\"\\0\"" target="\"_blank\"">\\0</a>",</p> |
Any idea what is wrong? |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Fri Jan 20, 2017 7:07 am Post subject: |
|
|
The value may need to be other than a straight PHP syntactically-correct function call. Maybe more like Smarty's variable modifier syntax.
So,
Code: | {assign var="string" value=$string|preg_replace:"#([[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/])#":"<a href=\"\\0\" TARGET=\"_blank\">\\0</a>"} |
But, since this is a modifier acting on the same variable, we can apply it to that variable and just use it:
Code: | {$string|preg_replace:"#([[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/])#":"<a href=\"\\0\" TARGET=\"_blank\">\\0</a>"} |
|
|
Back to top |
|
primozs Smarty Rookie
Joined: 14 Jan 2017 Posts: 8
|
Posted: Fri Jan 20, 2017 9:42 am Post subject: |
|
|
In both cases it doesn't work and I get the error:
WARNING: preg_replace(): Delimiter must not be alphanumeric or backslash |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Fri Jan 20, 2017 3:13 pm Post subject: |
|
|
We are closer.
It is strange because we are not using an alphanumeric character as the delimiter. We are using the # character.
So, I wonder if Smarty uses the arguments in the other order:
Code: | {$string|preg_replace:"<a href=\"\\0\" TARGET=\"_blank\">\\0</a>":"#([[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/])#"} |
|
|
Back to top |
|
primozs Smarty Rookie
Joined: 14 Jan 2017 Posts: 8
|
Posted: Fri Jan 20, 2017 6:06 pm Post subject: |
|
|
I know we are very close, thank you for helping.
Unfortunately the suggestion doesn't work. Again error "WARNING: preg_replace(): Delimiter must not be alphanumeric or backslash" |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Fri Jan 20, 2017 8:53 pm Post subject: |
|
|
For reasons I do not understand, using PHP's preg_replace() is not doing it for us.
Let's use Smarty's function:
Code: | {$string|regex_replace:"#([[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/])#":"<a href=\"\\0\" TARGET=\"_blank\">\\0</a>"} |
|
|
Back to top |
|
primozs Smarty Rookie
Joined: 14 Jan 2017 Posts: 8
|
Posted: Sat Jan 21, 2017 6:35 am Post subject: |
|
|
Great, now it works. Thank you! |
|
Back to top |
|
|