By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,719 Members | 1,466 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,719 IT Pros & Developers. It's quick & easy.

How to make text area recognize hyperlinks?

P: n/a
I built a simple form where a user can enter and post news items to his
site (with PHP, into MySQL). He doesn't know much HTML.

Is there some way he can enter some simple text to declare something a
hyperlink, and have PHP recognize that that means "make this text into a
hyperlink"?

....so that instead of having to type this into the textarea:

<a href="http://www.somesite.com">somesite</a>

....he could type something more intuitive, like this:

<somesite.com>somesite</somesite.com>?

Maybe there's a better way even than I'm explaining. Any help is
appreciated.
--Mike
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Mike Mella wrote:
....so that instead of having to type this into the textarea:

<a href="http://www.somesite.com">somesite</a>

....he could type something more intuitive, like this:

<somesite.com>somesite</somesite.com>?

Maybe there's a better way even than I'm explaining. Any help is
appreciated.


The easiest to use format is one where you don't get to specify what the
title of the link should be:

<link>http://www.somesite.com</link>

You could also use:

<link somesite.com/www/myfile.php?id=5>Click here</link>

With the format you suggest, you would require:

<somesite.com/www/myfile.php?id=5>Click here to visit
site</somesite.com/www/myfile.php?id=5>

In any case, you should look into regular expressions to parse your links.

For this format:

http://www.mylink.com

You can use the regex I'm using on my forum:

preg_replace(
"/\[url\](.*?)\[\/url\]/i",
"<a href='\\1'>\\1</a>",
$mytext);

Cheers,
Nicholas Sherlock
Jul 17 '05 #2

P: n/a
Nicholas Sherlock wrote:
Mike Mella wrote:
....so that instead of having to type this into the textarea:

<a href="http://www.somesite.com">somesite</a>

....he could type something more intuitive, like this:

<somesite.com>somesite</somesite.com>?


In any case, you should look into regular expressions to parse your links.

For this format:

http://www.mylink.com

You can use the regex I'm using on my forum:

preg_replace(
"/\[url\](.*?)\[\/url\]/i",
"<a href='\\1'>\\1</a>",
$mytext);


There's a lot of 'off the shelf' code to do this kind of thing. phpBB is the
most popular (and its secure for now). Alternatively if you just plonk
something like FCKeditor on the page then it's all done at the browser end
- although you'd probably want to do a lot of parsing of the returned value
to protect against XSS attacks.

Alternatively you might want to look at implementing a Wiki.

HTH

C.
Jul 17 '05 #3

P: n/a
Mike Mella wrote:
I built a simple form where a user can enter and post news items to his
site (with PHP, into MySQL). He doesn't know much HTML.

Is there some way he can enter some simple text to declare something a
hyperlink, and have PHP recognize that that means "make this text into a
hyperlink"?

...so that instead of having to type this into the textarea:

<a href="http://www.somesite.com">somesite</a>

...he could type something more intuitive, like this:

<somesite.com>somesite</somesite.com>?

Maybe there's a better way even than I'm explaining. Any help is
appreciated.
--Mike


Yeah, there is a better way: In this case, it sounds like your client
would very much appreciate using a WYSIWYG textarea editor, such as
TinyMCE: http://tinymce.moxiecode.com/. TinyMCE's a drop-in javascript
replacement compatible with most browsers, and is in fact already used
by many CMS's (such as MamboCMS). Very nice. I've used it to add a touch
of (optional) "class" to my projects.
Jul 17 '05 #4

P: n/a
Hey, that's great, thanks! I've set up TinyMCE and it is better.

I have a question for you since you've used it though -- When I type
something in the text field, TinyMCE automatically enters it between <p>
tags. I would like to change this. I understand that there is a file
somewhere where defaults like that can be edited, but the manual isn't
specific about which one. Can you tell me? I'll post it in the forums,
but I thought I'd ask you first.

Thanks again.
--Mike


Jason F. wrote:
Mike Mella wrote:
I built a simple form where a user can enter and post news items to
his site (with PHP, into MySQL). He doesn't know much HTML.

Is there some way he can enter some simple text to declare something a
hyperlink, and have PHP recognize that that means "make this text into
a hyperlink"?

...so that instead of having to type this into the textarea:

<a href="http://www.somesite.com">somesite</a>

...he could type something more intuitive, like this:

<somesite.com>somesite</somesite.com>?

Maybe there's a better way even than I'm explaining. Any help is
appreciated.
--Mike

Yeah, there is a better way: In this case, it sounds like your client
would very much appreciate using a WYSIWYG textarea editor, such as
TinyMCE: http://tinymce.moxiecode.com/. TinyMCE's a drop-in javascript
replacement compatible with most browsers, and is in fact already used
by many CMS's (such as MamboCMS). Very nice. I've used it to add a touch
of (optional) "class" to my projects.

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.