News:

Wondering if this will always be free?  See why free is better.

Main Menu

Can someone tell me what I'm doing wrong here

Started by Biology Forums, October 01, 2014, 02:12:04 PM

Previous topic - Next topic

Biology Forums

I want to create a little mod that changes URLs outside the base URL into a no-follow. Here's what I've gotten so far (this is being included in Subs.php (version 1.x).

if(preg_match_all('#<a href="(.*?)</a>#i', $message, $urlz, PREG_SET_ORDER))
{
$nofollow = 'rel="nofollow" href=';

foreach($urlz as $b)
{
if(strpos($b[0], $_SERVER['SERVER_NAME']) !== false)
echo '';
else
$message = str_replace('href=', $nofollow, $message); // Replace the link in the message
}
}


What it's doing is it's replacing ALL urls, even those that are $_SERVER['SERVER_NAME']. I posted a similar thread to this last week, but I was trying to accomplish something different there, so don't confuse the two...

Kindred

you know.... there is already at least one mod that does this?

and, you also should know (because we have said it several dozen times) that doing what you are doing is actually going to PENALIZE your SEO?
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Biology Forums

Quote from: Kindred on October 01, 2014, 02:24:41 PM
you know.... there is already at least one mod that does this?

and, you also should know (because we have said it several dozen times) that doing what you are doing is actually going to PENALIZE your SEO?


That's debatable. Wikipedia has no-follow on all external links and it's still #1.

PS: I'm not here to get into a debate - just want to learn how to code a little.

margarett

You should echo/print_r the variables to be sure what is happening in every moment.

I honestly can't understand your logic.
By doing this
$message = str_replace('href=', $nofollow, $message)
You are replacing ALL occurrences of a "href" with your nofollow. Be it internal or external links, as long as at least an external URL is detected.
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Biology Forums

lol that's it, thanks. Will keep playing with it.

Biology Forums

Got a working version. If anyone wants to use it, feel free, if anyone wants to improve on it, feel free as well!

if(preg_match_all('#<a (.*?)</a>#i', $message, $urlz, PREG_SET_ORDER))
{
$nofollow = 'rel="nofollow"';

foreach($urlz as $b)
{
if(strpos($b[1], $_SERVER['SERVER_NAME']) !== false)
echo '';
else
$message = str_replace($b[1], $nofollow.' '.$b[1], $message); // Replace the link in the message
}
}

Advertisement: