Jump to content

uhg... this one makes my head hurt


testbot

Recommended Posts

hey everyone,

 

i'm more of a str_replace replace guy so the thought of the following made my head hurt. :lol:

 

this is where i'm at so far:

$url = geturl();
$lightboxer = 'Lightbox_' . $this->containerid;

$find = "<a href=\"$url/attachment.php?attachmentid=xxxx\">Attachment xxxx</a>";

$replace = "<a href=\"$url/attachment.php?attachmentid=xxxx\" id=\"attachmentxxx\" rel=\"$lightboxer\"><img title=\"Click image for larger version\" class=\"thumbnail\" style=\"\" border=\"0\"></a>";

 

it seems pretty simple but the xxxx can be any random number found in $find.  that's where i'm kinda lost so i'm here asking for a lil help from the pro's! :D

 

 

Link to comment
https://forums.phpfreaks.com/topic/192435-uhg-this-one-makes-my-head-hurt/
Share on other sites

try this one

$find = '/<a href="(.+)\/attachment\\.php\\?attachmentid=(\\d+)">Attachment \\2\\<\/a>/i';
$replace = '<a href="\\1/attachment.php?attachmentid=\\2" id="attachment\\2" rel="'.$lightboxer.'"><img title="Click image for larger version" class="thumbnail" style="" border="0"></a>';

$result = preg_replace($find, $replace, $str);

 

In case it's not working post error and post how exactly looks data that u are trying to match:

is it text file with bunch of url's line by line or what?

Sader, by using \\ you are essentially double escaping characters, meaning you expect the pattern to contain a literal backslash followed by a literal version of the backslash escaped character you wished to use. So for example \\d+ will match backslash followed by any number of d's. The only time you want to escape Regex backslash character sequences is when you use double quote delimiters for the PHP string (in this case $find).

 

Aside from that at a glance it looks ok.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.