Jump to content

How can I do this?


eMonk

Recommended Posts

I'm try to add target="_blank" in the following a href so the link opens in a new tab. How can this be done?

 

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

 

I tried <a href="http://\\2" target="_blank">\\2</a> but it didn't work.

Link to comment
Share on other sites

Hi eMonk,

 

Not sure where the first capture group fits in real life, but keeping it as is (only modifying Group 2), this works.

Note that the input string starts with a } to give the regex something that matches the first capture group you specified.

 

Input:

}www.test.com

 

Code:

<?php
$regex=',([[:space:]()[{}])(www\.[-a-z0-9@:%_\+.~#?&//=]+),i';
$string='}www.test.com';
echo htmlentities(preg_replace($regex,'\1<a href="http://\2" target="_blank">\2</a>',$string));
?>

 

Output:

}<a href="http://www.test.com" target="_blank">www.test.com</a>

 

If the first capture group is "old garbage", run this instead:

 

Input:

www.test.com

 

Code:

<?php
$regex=',(www\.[-a-z0-9@:%_\+.~#?&//=]+),i';
$string='www.test.com';
echo htmlentities(preg_replace($regex,'<a href="http://\1" target="_blank">\1</a>',$string));
?>

 

Output:

<a href="http://www.test.com" target="_blank">www.test.com</a>

 

Is this what you're looking for?

Link to comment
Share on other sites

I have it in the a function. Here's the complete code:

 

$description = $row['description'];

function autolink($description) {
$description = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', 
            '\\1<a href="http://\\2">\\2</a>', $description);
return $description; 
}

Link to comment
Share on other sites

Yes, so in your function, just pop in what I gave you.

Without doing anything else to your code:

 

function autolink($description) {
$regex=',([[:space:]()[{}])(www\.[-a-z0-9@:%_\+.~#?&//=]+),i';
$description = preg_replace($regex,'\1<a href="http://\2" target="_blank">\2</a>',$description);
return $description; 
}

 

Or, for the second version, in case Group 1 in your regex is leftover garbage from something else:

function autolink($description) {
$regex=',(www\.[-a-z0-9@:%_\+.~#?&//=]+),i';
$description = preg_replace($regex,'<a href="http://\1" target="_blank">\1</a>',$description);
return $description; 
}

 

 

 

Link to comment
Share on other sites

That worked but if the user enters in a url with http:// it doesn't get included in the a href link and appears as normal text.

 

Also for email links:

 

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

 

See any an needed stuff here? Thanks for your help!! I'll be purchasing a book or read online tutorials how this stuff works in the upcoming weeks. It looks confusing at first glance.

Link to comment
Share on other sites

That worked but if the user enters in a url with http:// it doesn't get included

 

Okay, you're changing the spec on me. ;)

Slight change to the regex then, with an optional http://

 

$regex=',(?:http://)?(www\.[-a-z0-9@:%_\+.~#?&//=]+),i';

 

See any unneeded stuff here?

 

I have to tell you that the movement is away from the ereg functions, now deprecated, and toward the preg functions.

In a future version of PHP, they will drop off.

 

I would guess that this would fix your code (didn't have time to test it though, past midnight here):

 

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

Let me know if that works for you.

Link to comment
Share on other sites

Without testing:

 

function autolink($description) {
$regex=',(http://)?(www\.[-a-z0-9@:%_\+.~#?&//=]+),i';
$description = preg_replace($regex,'<a href="http://\2" target="_blank">\1\2</a>',$description);
return $description; 
}

 

The http only gets inserted if the text if it's there originally.

If you want it there all the time:

 

function autolink($description) {
$regex=',(?:http://)?(www\.[-a-z0-9@:%_\+.~#?&//=]+),i';
$description = preg_replace($regex,'<a href="http://\1" target="_blank">http://\1</a>',$description);
return $description; 
}

 

 

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.