Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Creating hyperlinks in the text

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
primozs
Smarty Rookie


Joined: 14 Jan 2017
Posts: 8

PostPosted: Thu Jan 19, 2017 3:50 pm    Post subject: Creating hyperlinks in the text Reply with quote

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
View user's profile Send private message
bsmither
Smarty Pro


Joined: 20 Dec 2011
Posts: 246
Location: West Coast

PostPosted: Thu Jan 19, 2017 4:51 pm    Post subject: Reply with quote

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
View user's profile Send private message
primozs
Smarty Rookie


Joined: 14 Jan 2017
Posts: 8

PostPosted: Fri Jan 20, 2017 6:45 am    Post subject: Reply with quote

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:]]+://[^&lt;&gt;[:space:]]+[[:alnum:]/]","<a href="\&quot;\\0\&quot;" target="\&quot;_blank\&quot;">\\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:]]+://[^&lt;&gt;[:space:]]+[[:alnum:]/])#","<a href="\&quot;\\0\&quot;" target="\&quot;_blank\&quot;">\\0</a>",</p>


Any idea what is wrong?
Back to top
View user's profile Send private message
bsmither
Smarty Pro


Joined: 20 Dec 2011
Posts: 246
Location: West Coast

PostPosted: Fri Jan 20, 2017 7:07 am    Post subject: Reply with quote

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
View user's profile Send private message
primozs
Smarty Rookie


Joined: 14 Jan 2017
Posts: 8

PostPosted: Fri Jan 20, 2017 9:42 am    Post subject: Reply with quote

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
View user's profile Send private message
bsmither
Smarty Pro


Joined: 20 Dec 2011
Posts: 246
Location: West Coast

PostPosted: Fri Jan 20, 2017 3:13 pm    Post subject: Reply with quote

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
View user's profile Send private message
primozs
Smarty Rookie


Joined: 14 Jan 2017
Posts: 8

PostPosted: Fri Jan 20, 2017 6:06 pm    Post subject: Reply with quote

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
View user's profile Send private message
bsmither
Smarty Pro


Joined: 20 Dec 2011
Posts: 246
Location: West Coast

PostPosted: Fri Jan 20, 2017 8:53 pm    Post subject: Reply with quote

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
View user's profile Send private message
primozs
Smarty Rookie


Joined: 14 Jan 2017
Posts: 8

PostPosted: Sat Jan 21, 2017 6:35 am    Post subject: Reply with quote

Great, now it works. Thank you!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP