p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: links inside a text


Message #1 by "Rodrigo Penloski - Som Livre.com" <Rodrigo@s...> on Mon, 10 Feb 2003 15:59:14 -0300
Hi to all,
I have a field in my db, where it holds an entire article, so maybe in
the middle of the whole thing I'll need a link to somewhere, for
instance, if I'm talking about an artist and i want to link his name to
his official website, how can I do that, is it possible??

I'd appreciate any help...


Thanks,

penkz
Message #2 by "Pedro Graca" <bzzzt@f...> on Mon, 10 Feb 2003 19:47:01 +0000
On Mon, 10 Feb 2003 15:59:14 -0300, Rodrigo Penloski - Som Livre.com
said:
> Hi to all,
> I have a field in my db, where it holds an entire article, so maybe in
> the middle of the whole thing I'll need a link to somewhere, for
> instance, if I'm talking about an artist and i want to link his name to
> his official website, how can I do that, is it possible??
> 
> I'd appreciate any help...

you can try to adapt this script I did for the same thing.


You have to turn the artist's name and URL into something like this
[artist name|http://www.artistname.com/bio.html]
^___________^__________________________________^

The rule is to have the text for the link within [ and ],
name first without [, ], or |
and then the URL

hope this helps


<?php
  function makelinks($txt) {
    // 'cure' txt from HTMLisms
    $txt = htmlentities($txt, ENT_QUOTES);

    // valid chars in a URL
    $validchars = '-a-z0-9_+\.@\.../%$!*\'\(\);?:=&';

    $regexp = "/\[(.*)\|(http:\/\/[$validchars]+)\]/i";
    $subst = '<a href="$2">$1</a>';

    return preg_replace($regexp, $subst, $txt);
  }

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-==-=-

  // usage example
  $simpletext = "examples:
blah blah blah [Bad example|http://] blah blah blah
bleh bleh bleh [Another bad example|http://www] bleh bleh bleh
... [and yet another bad example|http://www.] ...
[the first good example|http://www.example.com]
[the second good example|http://www.example.com/]
[with a file name|http://www.example.com/show.php]
[and a wrong parameter|http://www.example.com/show.php#id=14]
[two good parameters|http://www.example.com/show.php?id=14&color=7]
[I'm runnin out of
texts...|http://www.example.com/path/show.php?id=14&color=7]
[wrong char in path|http://www.example.com/path\show.php?id=14&color=7]
[wrong char in parameter|http://www.example.com/search?q=regexp php]
[uffffffff! last one|http://www.example.com/search?q=regexp%20php]
";

  // print the original version
  print "<pre>" . $simpletext . "</pre>";

  // a separator
  print "\n\n<br /><hr><br />\n\n";

  // and the modified version
  print "<pre>" . makelinks($simpletext) . "</pre>";
?>
-- 
                                            ()    ribbon campaign     ()
                                            /\   against HTML mail    /\
Message #3 by "Juliet May" <jmay@s...> on Mon, 10 Feb 2003 12:48:14 -0700
You would need to include the actual html tags for the link with the rest of
the text in the field in your db. Basically you need to insert your html
code into the field instead of straight text.

Julie
----- Original Message -----
From: "Rodrigo Penloski - Som Livre.com" <Rodrigo@s...>
To: "beginning php" <beginning_php@p...>
Sent: Monday, February 10, 2003 11:59 AM
Subject: [beginning_php] links inside a text


Hi to all,
I have a field in my db, where it holds an entire article, so maybe in
the middle of the whole thing I'll need a link to somewhere, for
instance, if I'm talking about an artist and i want to link his name to
his official website, how can I do that, is it possible??

I'd appreciate any help...


Thanks,

penkz



  Return to Index