If all the pictures in $mydata do not have the "rel" attribute, then solution is simple
$mydata = str_replace('<img ', '<img rel="lightbox"', $mydata);
In other cases, only if ">" character does not appear in attribute values, this code works
function rel_adder( $matches ) {
if ( strpos($matches[0], 'rel=') === false ) {
return $matches[1].'rel="lightbox" '. $matches[2];
} else {
return $matches[0];
}
}
$mydata = preg_replace_callback('#(<img )([^>]+)>#i', "rel_adder", $mydata);