0

I need a regular expression to use in php that will add rel='lightbox[nason]' to the a tag where a string /gallery/dsc_ is found.

Example input string: http://myURL/gallery/dsc_8691-1-bw-3/' title='DSC_8691-1-bw'>

Example output string: http://myURL/gallery/dsc_8691-1-bw-3/' title='DSC_8691-1-bw' rel='lightbox[nason]'>

JDB
  • 25,172
  • 5
  • 72
  • 123
  • 1
    I see that you are new to StackOverflow. Welcome! It is generally recommended that new visitors review our [About](http://stackoverflow.com/about) page (we're a bit different from most Q/A sites). The best kinds of questions (which typically generate the most answers) usually include some sample code. It helps to see not only what you've tried, but also that you've put some effort into finding a solution on your own. – JDB Mar 14 '13 at 14:39
  • You may also find that parsing HTML with regex is generally not recommended. [It is very difficult to do well](http://stackoverflow.com/a/4234491/211627), and usually creates a brittle, difficult to maintain solution. You may find there are already [some great solutions](http://stackoverflow.com/q/292926/211627) for parsing HTML in PHP. – JDB Mar 14 '13 at 14:45

1 Answers1

1

Like that

$count = null;
$returnValue = preg_replace('~(a.*href=(?:"|\').*/gallery/dsc_.*(?:"|\'))~U', '$1 rel="lightbox[nason]"', '<a href="http://myURL/gallery/dsc_8691-1-bw-3/" title="DSC_8691-1-bw">', -1, $count);
GoT
  • 156
  • 4