Get the title attribute into a lightbox frame
There are multiple issues for Lightbox about your problem. Atm your best chance is indeed to override the relevant tpl.php file. This would be in the form of views-view-field--viewid--fieldname.tpl.php. The variables exposed in there that are of interest are the following:


$output: The processed output that will normally be used.
$row: The raw SQL result that can be used.


The first one indeed contains a string in the form of:

&lt;a 
  href="http://www.example.com/sites/default/files/styles/large/public/img.png?itok=Tn9Y1Pyo" 
  rel="lightbox[field_image][]"&gt;
    &lt;img 
      typeof="foaf:Image" 
      src="http://www.example.com/sites/default/files/styles/lines_thumbnail/public/img.png?itok=shRRK" alt="" /&gt;
&lt;/a&gt;


The above should become something like this:

&lt;a 
  href="http://www.example.com/sites/default/files/styles/large/public/img.png?itok=Tn9Y1Pyo" 
  rel="lightbox[field_image][&lt;p&gt;CAPTION&lt;/p&gt;]"&gt;
    &lt;img 
      typeof="foaf:Image" 
      src="http://www.example.com/sites/default/files/styles/lines_thumbnail/public/img.png?itok=shRRK" alt="" /&gt;
&lt;/a&gt;


in order to have a caption in your lightbox.

You could of course use a replace function in order to achieve this, such as your attempted solution.

What I recommend is to use the $row variable to obtain the data itself and construct the output however you wish, for instance to add a title in &lt;a&gt; as well and why not solve the empty alt attribute value issue as well for the sake of SEO:

&lt;?php
  //print $output; 
  //dpm($output); 
  //dpm($row);
  $caption = $row-&gt;taxonomy_term_data_name, // HERE use your field of choice
  print l(
    theme_image_style(array(
      'style_name' =&gt; 'thumbnail',
      'path' =&gt; $row-&gt;field_field_image[0]['raw']['uri'],
      'alt' =&gt; $caption,
      'attributes' =&gt; array('typeof' =&gt; 'foaf:Image'),
      'width' =&gt; NULL,
      'height' =&gt; NULL,
    )),
    image_style_url('large', $row-&gt;field_field_image[0]['raw']['uri']),
    array(
      'attributes' =&gt; array(
        'title' =&gt; $caption,
        'rel' =&gt;
          'lightbox[field_image][&lt;p&gt;' . $caption . '&lt;/p&gt;]',
      ),
      'html' =&gt; TRUE,
    )
  );
?&gt;