How to make text area recognize hyperlinks?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Mella

    #1

    How to make text area recognize hyperlinks?

    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.co m">somesite</a>

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

    <somesite.com>s omesite</somesite.com>?

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

    #2
    Re: How to make text area recognize hyperlinks?

    Mike Mella wrote:[color=blue]
    > ....so that instead of having to type this into the textarea:
    >
    > <a href="http://www.somesite.co m">somesite</a>
    >
    > ....he could type something more intuitive, like this:
    >
    > <somesite.com>s omesite</somesite.com>?
    >
    > Maybe there's a better way even than I'm explaining. Any help is
    > appreciated.[/color]

    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.co m</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:



    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

    Comment

    • Colin McKinnon

      #3
      Re: How to make text area recognize hyperlinks?

      Nicholas Sherlock wrote:
      [color=blue]
      > Mike Mella wrote:[color=green]
      >> ....so that instead of having to type this into the textarea:
      >>
      >> <a href="http://www.somesite.co m">somesite</a>
      >>
      >> ....he could type something more intuitive, like this:
      >>
      >> <somesite.com>s omesite</somesite.com>?[/color]
      >
      > 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);
      >[/color]

      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.

      Comment

      • Jason F.

        #4
        Re: How to make text area recognize hyperlinks?

        Mike Mella wrote:[color=blue]
        > 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.co m">somesite</a>
        >
        > ...he could type something more intuitive, like this:
        >
        > <somesite.com>s omesite</somesite.com>?
        >
        > Maybe there's a better way even than I'm explaining. Any help is
        > appreciated.
        > --Mike[/color]

        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.

        Comment

        • Mike Mella

          #5
          Re: How to make text area recognize hyperlinks?

          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:[color=blue]
          > Mike Mella wrote:
          >[color=green]
          >> 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.co m">somesite</a>
          >>
          >> ...he could type something more intuitive, like this:
          >>
          >> <somesite.com>s omesite</somesite.com>?
          >>
          >> Maybe there's a better way even than I'm explaining. Any help is
          >> appreciated.
          >> --Mike[/color]
          >
          >
          > 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.[/color]

          Comment

          Working...