$re = '/(?:#|//)[^\r\n]*|/\*[\s\S]*?\*//';
$str = 'mystring bla bla bla <-- should find this
bla bla mystring bla <-- also this
// bla bla mystring <-- not this , because is already commented
//mystring <-- not this
// alkdfjñas askfjña bla bla mystring <-- not this
wsfier mystring añljkfasñf <--should find this
mystring //a comment <-- should find this
bla bla // asfsdf mystring <-- should SKIP this, because mystring is commented
/*
asdfasf
mystring <-- i dont care if it finds this, even if it is inside a block comment
añfkjañsflk
// aksañl mystring <-- but should skip this, because the single line is already commented with \'//\' (regardless the block comment)
añskfjñas
asdasf
*/';
preg_match($re, $str, $matches, PREG_OFFSET_CAPTURE, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php