Regex challenge

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
zoransoko
Forum Newbie
Posts: 2
Joined: Tue Jan 31, 2012 7:50 pm

Regex challenge

Post 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
abareplace
Forum Newbie
Posts: 9
Joined: Fri Jan 06, 2012 1:43 am

Re: Regex challenge

Post 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);

?>
zoransoko
Forum Newbie
Posts: 2
Joined: Tue Jan 31, 2012 7:50 pm

Re: Regex challenge

Post by zoransoko »

This should do it. Thanks!
Post Reply