preg_replace ... a little more help

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

Moderator: General Moderators

Post Reply
ska
Forum Commoner
Posts: 41
Joined: Mon Sep 05, 2005 4:54 pm

preg_replace ... a little more help

Post by ska »

Hello, I'm back.

Following on from a previous post, I now need to do a preg_replace. The code I have written so far (which doesn't quite work):

Code: Select all

for ($i=0; $i<count($imagematch[0]); $i++) {
		
		$result = preg_replace('/'.$imagematch[1][$i].'.jpg" border="1" width="120" height="([^\]]+)" alt="View full details of this Property"><\/a>/i', $imagematch[1][$i].'.jpg" border="1" width="120" height="\\1" alt="View full details of this Property"></a><br><br><a href="arrange-viewing.php?pid='.$matches[2][$j].'"><img src="/images/arrange-viewing.gif" width="118" height="25" border="0"></a>', $result);
		
		$j=$j+2;
	}
Without worrying too much about the loop, how does the preg_replace look to you? I'm basically looking for an instance of an image and then adding a button after the image. The button contains some code I have extracted in a previous preg_match and is stored in $matches[2][$i]. The width of the image is always 120, but the height varies, hence the need for a preg_replace rather than a straight str_replace.

Thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

since height can, legally only be a number....

Code: Select all

for ($i=0; $i<count($imagematch[0]); $i++) {
        
        $result = preg_replace('/'.$imagematch[1][$i].'.jpg" border="1" width="120" height="([0-9]*?)" alt="View full details of this Property"><\/a>/i', $imagematch[1][$i].'.jpg" border="1" width="120" height="\\1" alt="View full details of this Property"></a><br><br><a href="arrange-viewing.php?pid='.$matches[2][$j].'"><img src="/images/arrange-viewing.gif" width="118" height="25" border="0"></a>', $result);
        
        $j=$j+2;
    }
ska
Forum Commoner
Posts: 41
Joined: Mon Sep 05, 2005 4:54 pm

Post by ska »

Thanks. However, in this case the height can have a decimal place. (eg. 34.21) Yes, I know that's weird! I'm not sure if this is a regex problem so much as my looping perhaps not working correctly. My basic aim is to find a code which is on the page which is unique to each image. (eg. SP3 is relevant to image PRP1231231.jpg) I then need to find each instance of an image and add a button underneath the the image in which the href contains the unique code. What I have at the moment is:

Code: Select all

//gets the unique code
preg_match_all('#(["\'])'.preg_quote('http://www.domainname.com/cgi-bin/operate.pl?&prp1&&','#').'(.*?)\\1#',$result,$matches);

//gets the image name. $agentcode is a global variable set earlier on
preg_match_all ('/'.$agentcode.'\/images_p\/*([0-9a-zA-Z]{10})*.jpg/Uis', $result, $imagematch);

//now loop through each instance of an image, and add the button underneath it
$j=0;
for ($i=0; $i<count($imagematch[0]); $i++)
{
	$result = preg_replace('/'.$imagematch[1][$i].'.jpg" border="1" width="120" height="([^\]]+)" alt="View"><\/a>/i', $imagematch[1][$i].'.jpg" border="1" width="120" height="\\1" alt="View"></a><br>'.$imagematch[1][$i].'<br><a href="arrange-viewing.php?pid='.$matches[2][$j].'"><img src="/images/arrange-viewing.gif" width="118" height="25" border="0"></a>', $result);
      
       // there are two instances of the unique code on the page, so you have to skip through the array in twos
	$j=$j+2;
		
}
This is partly working. What is happening though, is that the correct number of buttons are being added with the correct href, but only after the last instance of the image. So say there are three images and three unique codes, the above will reproduce the three images, and after the last of these, three buttons will appear. But there should be one button per image. It's confusing me severly, and I've echoed my variables out in the loop to see if I can spot it, but it's illuding me so far.

Any comments would be very welcome. Let me know if I'd be better putting this in the main PHP forum though.
Post Reply