public static function linkXHTMLSafe($input)
    {
        $regex = 'href="([^"]*(&(amp;){0})[^"]*)*?"';

        return preg_replace_callback(
            "#$regex#i",
            function ($m) {
                $rx = '&(?!amp;)';

                return preg_replace('#' . $rx . '#', '&amp;', $m[0]);
            },
            $input
        );
    }
public static function linkXHTMLSafe($input)
    {
        $regex = 'href="([^"]*(&(amp;){0})[^"]*)*?"';

        return preg_replace_callback(
            "#$regex#i",
            function ($m) {
                $rx = '&(?!amp;)';

                return preg_replace('#' . $rx . '#', '&amp;', $m[0]);
            },
            $input
        );
    }
public static function linkXHTMLSafe ( $ input ) { $ regex = 'href="([^"]*(&(amp;){0})[^"]*)*?"' ; return preg_replace_callback ( "#$regex#i" , function ( $ m ) { $ rx = '&(?!amp;)' ; return preg_replace ( '#' . $ rx . '#' , '&amp;' , $ m [ 0 ] ) ; } , $ input ) ; }
This method processes a string and replaces all instances of & with &amp; in links only.

@param   string $input String to process

@return  string  Processed string
This method processes a string and replaces all instances of & with &amp ; in links only .