Jump to content

Preg_Replace


ajicles

Recommended Posts

I have a problem, I want to use PHP to search through a variable and take the text and make it into <a href> click-able link. But the only thing is there is a lot of other things inside of the same variable.

 

I could use this but, it just only works with one thing in the variable:

 

<?php

function clickable_link($text)
{

$text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $text);

$ret = ' ' . $text;

$ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);

$ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);

$ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);

$ret = substr($ret, 1);
return $ret;
}
$text="www.google.com";

echo clickable_link($text); 
?>

Link to comment
Share on other sites

I could use this but, it just only works with one thing in the variable:

 

Huh? With  $text="www.google.com is fun. ftp.goobar.com is not.  http://link.me for short links!"; all three are wrapped in anchor tags as expected.

Link to comment
Share on other sites

You may find this link about laziness and greediness of help.

 

http://www.phpfreaks.com/forums/index.php/topic,211115.msg963061.html#msg963061

 

You want to expressions to be greedy.

 

I could do this but I would have to make custom tags like <link> </link> and echo back the url. But with a chunk of text I would have to search for the beginning of http and follow that to the end of the url. Something like:

preg_match('%http://(.+?) %s', $data, $matches);

 

and the space after the url ends the link which would be proper? I'll try this and get back to you.

Link to comment
Share on other sites

I tried using:

 

<?php
$data = "
Everthing you need to know about LGU:
http://www.legitgamingunion.com/

Web Designer:
http://www.youtube.com/user/xIVIAJIKx

Intro:
http://www.youtube.com/user/dougangot...

GFX:
http://www.youtube.com/user/BazikG

ROK:
http://www.youtube.com/user/R0KL0bStEr";

preg_match('%http://(.+?)
%s', $data, $matches);

echo $matches[1];
?>

 

Still trying to figure this out. I use from http:// to a line break but I was wondering if I could use a for statement to get all the of the urls..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.