Page 1 of 1

Regex challenge

Posted: Tue Jan 31, 2012 8:07 pm
by zoransoko
Hello,

Can anyone do this regex task?
First here is the string:



<P ALIGN="LEFT">
<FONT FACE="Times New Roman" style="font-size:2px" COLOR="#000000" LETTERSPACING="0" KERNING="0">
<A HREF="javascript: lightbox('');">
<IMG SRC="images/pages/abandoned_beauty.jpg" ID="$img3" ALIGN="left" VSPACE="8" HSPACE="8">
<IMG SRC="images/pages/abandoned_beauty.jpg" ID="$img4" ALIGN="left" VSPACE="8" HSPACE="8">
</A>
</FONT>
</P>


<P ALIGN="LEFT">
<FONT FACE="Times New Roman" style="font-size:12px" COLOR="#000000" LETTERSPACING="0" KERNING="0">
</FONT>
</P>

<P ALIGN="LEFT">
<FONT FACE="Times New Roman" style="font-size:2px" COLOR="#000000" LETTERSPACING="0" KERNING="0">
<A HREF="http://nekisajt.com" TARGET="_blank">
<IMG SRC="images/pages/close_to_heart.jpg" ID="$img5" ALIGN="left" VSPACE="8" HSPACE="8">
</A>
</FONT>
</P>



<P ALIGN="LEFT">
<FONT FACE="Times New Roman" style="font-size:12px" COLOR="#000000" LETTERSPACING="0" KERNING="0">
</FONT>
</P>

<P ALIGN="LEFT">
<FONT FACE="Times New Roman" style="font-size:2px" COLOR="#000000" LETTERSPACING="0" KERNING="0">
<A HREF="javascript: lightbox('');">
<IMG SRC="images/pages/fresh_picked.jpg" ID="$img6" ALIGN="left" VSPACE="8" HSPACE="8">

</A>
</FONT>
</P>



Second, I need to extract 'SRC' value from 'IMG' tag and place it between single quotes in lightbox('') which is inside 'HREF' tag of it's parent 'A' tag (and only it's parent 'A' tag - so this is the rule).

Third, source string is case sensitive and must be exactly like it is placed here with all new lines and silly upper case HTML tags :)

Any idea would be appreciated.
Sorry, but I needed to just paste the target string here without put it in the BBCode Text tag because some characters were changed in BBCode.

Zoran

Re: Regex challenge

Posted: Tue Jan 31, 2012 9:11 pm
by abareplace
In a search-and-replace tool:

Code: Select all

Search for:   <A HREF="javascript: lightbox\(''\);">\s*(<IMG SRC="([^"]+)" ID="[^"]+" ALIGN="left" VSPACE="8" HSPACE="8">)
Replace with: <A HREF="javascript: lightbox('\2');">\1
In PHP:

Code: Select all

<?php

$target = file_get_contents('text.htm'); // Your string

$search_for = ',<A HREF="javascript: lightbox\\(\'\'\\);">
(<IMG SRC="([^"]+)" ID="[^"]+" ALIGN="left" VSPACE="8" HSPACE="8">),';

$replace_to = '<A HREF="javascript: lightbox(\'$2\');">
$1';

echo preg_replace($search_for, $replace_to, $target);

?>

Re: Regex challenge

Posted: Tue Jan 31, 2012 10:51 pm
by zoransoko
This should do it. Thanks!