Jump to content

[SOLVED] Need help with replacing strings on external links


DWMeso

Recommended Posts

I've been trying to figure out a way to make this work with Wordpress all day and I cant figure it out for anything.

 

Basically what I'm trying to do is  replace < a href="#"  with < a rel="nofollow" href="#"

 

but only for external links... anyone know how I could get this to work? I'm not skilled enough to create the script from scratch and I'm having trouble finding any answers

 

What i'm trying to do:

<?php 
if ( blah blah ) {

PHP STR REPLACE CODE HERE

}
?>

 

I know there are wordpress plugins to make all external links nofollow, but that's not what i'm trying to do. I only need links to be nofollow if they fall into the IF..

 

it doesn't have to be an str replace code.. anything that does the job will work

 

Any help will be very appreciated

 

 

Link to comment
Share on other sites

String replace works with Strings.

 

<?php
// Provides: <body text='black'>
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");

// Provides: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");

// Provides: You should eat pizza, beer, and ice cream every day
$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy   = array("pizza", "beer", "ice cream");

$newphrase = str_replace($healthy, $yummy, $phrase);

// Use of the count parameter is available as of PHP 5.0.0
$str = str_replace("ll", "", "good golly miss molly!", $count);
echo $count; // 2

// Order of replacement
$str     = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
$order   = array("\r\n", "\n", "\r");
$replace = '<br />';
// Processes \r\n's first so they aren't converted twice.
$newstr = str_replace($order, $replace, $str);

// Outputs: apearpearle pear
$letters = array('a', 'p');
$fruit   = array('apple', 'pear');
$text    = 'a p';
$output  = str_replace($letters, $fruit, $text);
echo $output;
?> 

 

If you need to change things actively on a page as it loads you can try to use JavaScript

Link to comment
Share on other sites

I don't just need to replace a string though... there are plenty of topics covering how to do that. As i said, i need to replace a string in external links only, not every link on the page.. I still can't find any resources on how to do this...

 

The script needs to check if the link is internal (pointed to a page within the website) or external, pointing to a different domain.. once it knows if it's internal or external, it should replace the string only on external links...

 

Let me know if I'm not making sense and I can try and explain it better.

 

Thanks for trying to help

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.