/    Sign up×
Community /Pin to ProfileBookmark

Form Text into Hypelinks

If you had a text area that contained [LINK=some url]some website[/LINK], how would you write a PHP script to convert that into a clickable link when the form data was output into the page?

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@scragarAug 15.2008 — [code=php]preg_replace("/[link=(.*?)](.*?)[/link]/i", "<a href='$1'>$2</a>", $str);[/code]untested, but it all works out logically.
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorAug 15.2008 — [code=php](.*?) $1 $2[/code]

What are those bits telling it to do?
Copy linkTweet thisAlerts:
@scragarAug 15.2008 — ( )</URL>bits of a regexp inside brackets get stored as $1 for the first, then $2 for the second and so on up to 99
.repesents any character
*say's any number, I should really have used+to say 1 or more, edit that.
?normaly means optional, but when right after a * or + it turns greedy search off, meaning that it matches the smallest string it can, rather than matching the largest it can:[code=php]echo preg_replace("/a.*c/", "d", 'abc-abc');// greedy on, result is 'd'
echo preg_replace("/a.*?c/", "d", 'abc-abc');// greedy off, result is 'd-d'[/code]
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorAug 15.2008 — bits of a regexp inside brackets get stored as $1 for the first, then $2 for the second and so on up to 99[/QUOTE]

Sorry, but I don't understand what you mean?

smallest string it can, rather than matching the largest it can[/QUOTE]

How is size calculated?
Copy linkTweet thisAlerts:
@scragarAug 15.2008 — Sorry, but I don't understand what you mean?
[/quote]
I'm not very good at explaining this stuff, but for each set of brackets the preg_replace function uses a $n variable for it, so $1 is the first set of brackets, $2 is the second... $99 is the 99th set of brackets. It only allows 99 sets of brackets mapped in this way(which is not to say you can't have more than 99 sets of brackets, only that you can only use the $n variables for the first 99).

http://php.net/preg_replace -- read the manual(skip to replacement in the parameters section) if you still don't understand what I mean, although I personally don't think it explains it much better.

How is size calculated?[/QUOTE]length.
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorAug 15.2008 — I think I get it now. Thanks!

If you were outputting a rather large string (like a message board post), how could you use that function multiple times for features in addition to hyperlinks, such as bold text, larger text, etc.?
Copy linkTweet thisAlerts:
@scragarAug 15.2008 — it allows array inputs, so build an array:
[code=php]$from = array(
"/[link=(.*?)](.*?)[/link]/i",
"/[b](.*?)[/b]/i",
"/[u](.*?)[/u]/i",
"/[i](.*?)[/i]/i"
);
$to = array(
"<a href='$1'>$2</a>",
"/<b>$1</b>",
"/<u>$1</u>",
"/<i>$1</i>"
);

echo preg_replace($from, $to, $message);
[/code]
Copy linkTweet thisAlerts:
@gersompieAug 15.2008 — Works eregi_replace with arrays to? hm.. interesting ?
×

Success!

Help @Joseph_Witchard spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...