Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+7 votes
in Q2A Core by

I tried the following without succes, anybody know what is wrong in this code:

This is what I have now:
function qa_html_convert_urls($html, $newwindow=false)
    Return $html with any URLs converted into links (with nofollow and in a new window if $newwindow)
    URL regular expressions can get crazy: http://internet.ls-la.net/folklore/url-regexpr.html
    So this is something quick and dirty that should do the trick in most cases
        return trim(preg_replace('/([^A-Za-z0-9])((http|https|ftp):\/\/([^\s&<>"\'\.])+\.([^\s&<>"\']|&amp;)+)/i', '\1<A HREF="\2" rel="nofollow"'.($newwindow ? ' target="_blank"' : '').'>\2</A>', ' '.$html.' '));
 // remove nofollow for my links
    $search = '/href="http:\/\/YOURDOMAIN\/([^"]*)" rel="nofollow"/i';
    $replace = 'href="http://YOURDOMAIN/\1"';       
    $html = trim( preg_replace($search, $replace, $html) );
    return $html;

This apparently does not work.


2 Answers

+4 votes
edited by
Best answer

I actually do this within my theme now, which means it will survive upgrades. I'll post instructions here.

1. Create a custom theme if you don't already have one.

2. In qa-theme.php, add this function in the qa_html_theme class, replacing EXAMPLE.COM with your domain:

function _remove_nofollow( $str )

    $search1 = '#<a rel="nofollow" href="http://EXAMPLE.COM("|/[^"]*")#i';
    $search2 = '#<a href="http://EXAMPLE.COM("|/[^"]*") rel="nofollow"#i';
    $replace = '<a href="http://EXAMPLE.COM\1';
    $str = preg_replace( $search1, $replace, $str );
    $str = preg_replace( $search2, $replace, $str );
    return $str;

3. Override the q_view_contenta_item_content and c_item_content functions to call this function:

function q_view_content($q_view)
    if ( !empty($q_view['content']) )
        $q_view['content'] = $this->_remove_nofollow( $q_view['content'] );
function a_item_content($a_item)
    if ( !empty($a_item['content']) )
        $a_item['content'] = $this->_remove_nofollow( $a_item['content'] );
function c_item_content($c_item)
    if ( !empty($c_item['content']) )
        $c_item['content'] = $this->_remove_nofollow( $c_item['content'] );

Hope this helps!

UPDATED: fixes for comments where nofollow gets added at the end for some reason, and slightly cleaner code.

Thanks for this hack, it certainly works...but i found it is not removing the urls for files or attachments. Is it ok or i have write extra piece of code for the attachments
@Kai, I'm not sure why this causes the indentation problem, as all output is sent through the output function as it should be.
I tested it, with the hack I got the "html message" without the hack, no message. Strange to me as well, maybe this has to do with unicode stuff? I have no idea...
0 votes

you can use Q2A SEO Links plugin.

This worked for me...! Moreover, we can create website wise rule for nofollow, dofollow, etc. It will be helpful for own external website.