Jump to content

Recommended Posts

Hi,

 

Can you guys help me giving a function that takes all the contents in a particular field and returns text and url seperately...

 

For instance,

 

Go:

here is the link

www.google.com

 

it takes all the contents in the Go,and returns "here is the link" as text and "www.google.com" as link.I've an idea that preg match has something to do with content matching,I've tried that and I gave up.

 

 

Any help would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/192433-function-to-return-url/
Share on other sites

I've seen that thread and thats not wroking in my case....

here is my funcation takes the text and which should display the text as a text and url as a hyperlink...but this displays both text as well as url as a hyperlink....

 

function make_url_link($url)

{

if(empty($url)) {

        return;

    }

$url= preg_replace("/([^A-z0-9])(http|ftp|https)([\:\/\/])([^\\s]+)/","<a href=\"$2$3$4\">$2$3$4</a>",$url);

    return '<a href="'.$url.'">'.$url.'</a>';

}

 

 

any suggestions??

Thanks it worked... :)

 

Could you tell me what exactly what exactly the content means inside the replace function...I've some idea that it replaces the url with hyperlink...I'm confused of all the letters and symbols inside the function...Also,could you help me finding some sources on regex and the stuff inside which does the actual trick...

 

Maddali

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

 

I'll make it a bit simple.

% is a regex delimiter that must begin and end the regex. It does not need to be % it can be anything that is not alph-numeric and IS NOT used within the regex. So in this case I used %

\b is a word boundary, we take it all as one chunk of a word

(X|Y) means X or Y

so we match http or https

followed by ://

then followed by [] which denotes a class

meaning only what is in the class will be matched.

the last part is the i at the end, means case insensitive

The significant part is that the classes are missing

\S

which is a space, so it matches anything in the class unitl a space

then stops. because it's a replace it has whats called "look back"

whick looks back at what it matched and spits it out.

 

if you look at the first example given there are a lot of round brackets() those cause specific look backs that are $n, corresponding to each successive set of round brackets.

in the second example since we have no round brackets(the first set does not count cause its sorta like a conditional(OR)) so with the built in look back we default to 0 for what we matched with the regex to replace.

 

HTH

Teamatomic

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.