Posted link converts to title of the link

Started on the old forum in April 2012 · view the original in the archive · old links redirect here

14 replies · 869 views · started 14 years ago · 1 min read

TL;DR · summary of 14 replies
  • - The code approach was to use file_get_contents and regex to extract the title of a URL from a user post.
  • - The problem with multiple links having the same title needs to be addressed, and it might require using a loop or separate replacements for each link.
  • - A possible solution is to use preg_match_all() instead of preg_replace(), which could help in handling multiple URLs.

Written automatically from the replies by a language model on our own server. It can be wrong; the posts below are the source.

5 participants
analyzer Developer · 104 posts #1

I have searched at g0ogle about this matter but i was not able to see the exact code i need. I want a script that everytime i post a link to a forum or shout box it views out the title of the link that enclosed by <title> and </title> an example is when i post http://php.net

the output will be the title of the url page of http://php.net

and the code will be Title of page here

instead of http://php.net

. Get my point? Can anyone give me a code?

It's n0t that i am afraid to die. Its just that if i die, wh0 wilL loVe her as muCh as i Do?

something else Fellow · 2,296 posts #2

same topic came up not to long ago and someone answered it in there

shushant Developer · 207 posts #3

something else wrote: same topic came up not to long ago and someone answered it in there

You'are About to Extract? title of Posted Link Just like fb do? Ya u Can Do Use file_get_contents nd use regxp to extract title

function page_title($url) {     $page = @file_get_contents($url);
if (empty($page)) return null;    $matches = array();
if (preg_match('/<title>(.*?)<\/title>/', $page, $matches))
{ 
return $matches[1];
}
else
{
return null;
}
}

usage

echo page_title("http://coding-talk.com/forum.php");
analyzer Developer · 104 posts #4

What if i post "please visit http://www.example.com?"

how it will be? I did not post a pure link. I can detect a url using a php script but my problem is how can i make the link clickable, you've just give me a code that detects the title. Can you give to me the continuation of the code that detects a url from a user post and make it clickable.

It's n0t that i am afraid to die. Its just that if i die, wh0 wilL loVe her as muCh as i Do?

something else Fellow · 2,296 posts #5

Search for auto url bb code its another topic that comes up quite a bit 🙂

shushant Developer · 207 posts #6

analyzer wrote: What if i post "please visit http://www.example.com?"

how it will be? I did not post a pure link. I can detect a url using a php script but my problem is how can i make the link clickable, you've just give me a code that detects the title. Can you give to me the continuation of the code that detects a url from a user post and make it clickable.

php - Turn a twitter hash tag into a link and links to clickable links - Stack Overflow

just_m3. Developer · 199 posts #7

analyzer wrote: What if i post "please visit http://www.example.com?"

how it will be? I did not post a pure link. I can detect a url using a php script but my problem is how can i make the link clickable, you've just give me a code that detects the title. Can you give to me the continuation of the code that detects a url from a user post and make it clickable.

<?php
function page_title($url) {     

if(preg_match('/http:\/\/www\./',$url)){ $ucc=preg_replace("/(.*?)www\./is","http://",$url); $linked=$ucc;}
else if (preg_match('/^www\./',$url)){$linked=$url;}
else { $linked=$url;}

$page = @file_get_contents($linked);
if (!$page) {$null="Website does not exist !"; return $null; }   

$matches = array();

if (preg_match('/<title>(.*?)<\/title>/', $page, $matches))
{ 
return $matches[1];
}

else
{
$null="Website does not exist !";
return $null;
}
}

function clicklinks($text)
{       
$regx1='/(((f|ht){1}tp:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/';
$regx2='/((^www.)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/';
$regx3='/([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/';

if (preg_match($regx1,$text,$title)){$linktitle = page_title($title[0]);}
else if (preg_match($regx2,$text,$title)){$linktitle = page_title($title[0]);}
else {if(preg_match($regx3,$text,$title)){$linktitle = page_title($title[0]);}}
	
$text = preg_replace($regx1.'i', '<a href="\\1">'.$linktitle.'</a>',$text); 
$text = preg_replace($regx2.'i', '<a href="http://\\1">'.$linktitle.'</a>',$text);     
$text = preg_replace($regx3.'i', '\\1<a href="http://\\2">'.$linktitle.'</a>',$text);
return $text;
}

echo clicklinks("there it has title from http://www.gowap.ro ");

?>

There you go 😛

accepts

http:// , http://www. , www. ,ftp:// 

hit tnx 😀 😊

This is ten percent luck, twenty percent skill Fifteen percent concentrated power of will Five percent pleasure, fifty percent pain 😂And a hundred percent reason to remember the name!

something else Fellow · 2,296 posts #8

looks like all the old auto url bb codes have been deleted so here is 1:

$in=array('`((?:https?|ftp)://\S+[[:alnum:]]/?)`si',
                  '`((?<!//)(www\.\S+[[:alnum:]]/?))`si');
                  $out=array('<a href="?go=out&site=$1">$1</a> ',
                  '<a href="?go=out&site=http://$1">$1</a>');
                  $text = preg_replace($in,$out,$text);

you will need to make it do separate preg replaces to use the get title code above though as it wont work in an array 😛

Added after 2 minutes:

just_m3 posted few secs before i did so i didnt see his post 🙂

just_m3. Developer · 199 posts #9

hmm

echo clicklinks("there is no more link title like http://gowap.ro ");

got to modd it x_X as it not gets title when combined with text ...

The script was updated 😀 ... check it ! now it works .. ;;) .. it is a little messy .. but it does the job !

This is ten percent luck, twenty percent skill Fifteen percent concentrated power of will Five percent pleasure, fifty percent pain 😂And a hundred percent reason to remember the name!

analyzer Developer · 104 posts #10

Thanky0u guys! It helps me a lot. . Sorry but i can't click the "thanks" button at this moment. . I am just using a phone to browse cd. But don't you worry, i will hit thanks later when i use my pc. . Thankyou again.

It's n0t that i am afraid to die. Its just that if i die, wh0 wilL loVe her as muCh as i Do?

analyzer Developer · 104 posts #11

@just_m3 i tried the code bro and it works. But there's a problem, i tried to put 2 different links but the title was the same. How will i'm going to fix the problem bro?

It's n0t that i am afraid to die. Its just that if i die, wh0 wilL loVe her as muCh as i Do?

just_m3. Developer · 199 posts #12

analyzer wrote: @just_m3 i tried the code bro and it works. But there's a problem, i tried to put 2 different links but the title was the same. How will i'm going to fix the problem bro?

Hmmm let me check it out 😀 ... he might require for each .. i've got an idea . tnx for posting this issue 😛

This is ten percent luck, twenty percent skill Fifteen percent concentrated power of will Five percent pleasure, fifty percent pain 😂And a hundred percent reason to remember the name!

analyzer Developer · 104 posts #13

Ok bro. . Please post it here if ever you solve the problem. . I think while loop can solve that. .

It's n0t that i am afraid to die. Its just that if i die, wh0 wilL loVe her as muCh as i Do?

arnage Lead · 811 posts #14

Just use preg_match_all() instead. 🙂

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
analyzer Developer · 104 posts #15

arnage where to put that preg_match_all() mate?

It's n0t that i am afraid to die. Its just that if i die, wh0 wilL loVe her as muCh as i Do?

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close