Use a positive lookahead to check for the links which are going to be matched wouldn't be followed by a closing php or code tag.
$content = <<<EOS
Link_1: [code]This is a textual line.
www.google.com
This is a textual line.[/code]
Link_2: [php]This is a textual line.
www.google.com
This is a textual line.[/php]
EOS;
$needle = '~(?:https?://)?(?:www\.)(?:[^.\s]+)(?:\.[^.\n\s]+)*\.\w{2,4}(?=(?:(?!\[/?(?:code|php)])[\S\s])*\[/(?:code|php)])~m';
echo preg_replace($needle,'LINK HIDDEN',$content);
Output:
Link_1: [code]This is a textual line.
LINK HIDDEN
This is a textual line.[/code]
Link_2: [php]This is a textual line.
LINK HIDDEN
This is a textual line.[/php]