Jump to content

Regex


Trykiz

Recommended Posts

Hi, I need help with regex. I need to replace all urls (http://example.com) with <a href='http://example.com'>http://example.com</a> this, but if

In text already exist <a href='http://example.com'>http://example.com</a> do nothing with it.

 

Example text:

 

Maecenas ornare viverra rhoncus http://google.com . Donec pulvinar ipsum nec felis mollis non iaculis turpis pellentesque <a href='http://example.com'>http://example.com</a> . Pellentesque vel convallis lacus <a href='http://www.phpfreaks.com'>phpfreaks</a> .

 

preg_replace('/(http:\/\/[a-zA-Z0-9.\/=@]+)/', '<a href=\\1>\\1</a>', $param); 

 

But it's replacing <a href='http://www.phpfreaks.com'>phpfreaks</a> to

 

<a href='<a href='http://www.phpfreaks.com'>http://www.phpfreaks.com</a>'>phpfreaks</a>

 

Thanks!

 

 

Sorry for wrong categorie :(

Link to comment
Share on other sites

Hi Trykiz,

 

Welcome to the phpfreaks forum!

 

Here's one way to deal with it.

 

Code:

<?php
$string='Maecenas ornare viverra rhoncus http://google.com . Donec pulvinar ipsum nec felis mollis non iaculis turpis pellentesque <a href=\'http://example.com\'>http://example.com</a> . Pellentesque vel convallis lacus <a href=\'http://www.phpfreaks.com\'>phpfreaks</a>';
$regex=',(http://[\w./=@?]++)(?![\'"<]),';
echo htmlentities(preg_replace($regex,'<a href="\\1">\\1</a>',$string)).'<br />'; 
?>

 

Output:

Maecenas ornare viverra rhoncus <a href="http://google.com">http://google.com</a> . Donec pulvinar ipsum nec felis mollis non iaculis turpis pellentesque <a href='http://example.com'>http://example.com</a> . Pellentesque vel convallis lacus <a href='http://www.phpfreaks.com'>phpfreaks</a>

 

After matching the url, the regex just checks (negative lookahead) that it is not followed by a quote or a < character. Here's more about regex lookarounds.

 

Let me know if you have any questions.

 

:)

Link to comment
Share on other sites

  • 2 weeks later...

try this

<?php
function getName($url) {
$input = trim($url, '/');

$urlParts = parse_url($input);

$domain = preg_replace('/^www\./', '', $urlParts['host']);

$parts = explode('.', $domain);

return $parts[0];
}

$d = 'http://www.google.com';


$tld = getName($d);
echo $tld;
?>

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.