Jump to content

Make website addresses into hyperlinks within a variable


mmsolutions

Recommended Posts

Dear all

 

I have the following code:

 

        <?php

        $query="select * from website_pages_sub WHERE subpage_id = ".$_SESSION['mainpage'] ." AND subpage_status LIKE '%live%'";

        $result = mysql_query($query);

        while ($row = mysql_fetch_array($result)) {

            ?>

 

<?php echo $row['content1';?>

 

 

 

Content1 is a field within a row in a table as you know but it contains lots of text like a new entry.

Within this field of text could be something like: "Contact the club website at http://www.google.co.uk"

 

I want code to assess the contents of this field and if it finds a website entry to turn it into a url.

 

Can this be done ?

 

Kind reagards

 

Mark

You'd want to use regexp for something like this.

 

This may be helpful:

 

preg_replace('\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]', '<a href="\0">\0</a>', $text);

 

Taken from: http://www.roscripts.com/PHP_regular_expressions_examples-136.html -- not tested.

 

Read up on preg_replace for how to use it properly.

So in the following instance, what do we replace $foo with ? is it $content ?

 

        <?php function autolink_test($foo)

{

// Modified from:  http://www.totallyphp.co.uk/code/convert_links_into_clickable_hyperlinks.htm

$foo = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1">\\1</a>', $page_content1);

if( strpos($foo, "http") === FALSE )

{

$foo = eregi_replace('(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="http://\\1" target=_blank rel=nofollow >\\1</a>', $page_content1);

}

else

{

$foo = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="http://\\2" target=_blank rel=nofollow >\\2</a>', $page_content1);

}

$foo = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', '<a href="mailto:\\1">\\1</a>', $page_content1);

return $foo;

}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.