testbot Posted February 17, 2010 Share Posted February 17, 2010 hey everyone, i'm more of a str_replace replace guy so the thought of the following made my head hurt. 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! Quote Link to comment https://forums.phpfreaks.com/topic/192435-uhg-this-one-makes-my-head-hurt/ Share on other sites More sharing options...
sader Posted February 17, 2010 Share Posted February 17, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/192435-uhg-this-one-makes-my-head-hurt/#findComment-1013966 Share on other sites More sharing options...
cags Posted February 17, 2010 Share Posted February 17, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/192435-uhg-this-one-makes-my-head-hurt/#findComment-1013981 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.