0

I have a piece of code as follows:

$query = preg_replace('/\<a href=\"(.*)\"\>\<img height=\"(\d+)\" border=\"0\" width=\"(\d+)\" alt=\"(.*)\" src=\"(.*)\" \/\>\<\/a\>/',"INSERT INTO newproducts (productName,supplierName,productLink,imageLink,groupCode,currency,price,updated) VALUES ('$4','supplier','$1','$5',",$ad);
		$query .= "'$group','\$',$price,CURRENT_DATE)";

which works fine. But I would like somehow to amend it so that the field productName ($4) can have slashes added to it where necessary, specifically where there are single quotes. It's not absolutely vital, but would definitely be helpful, as it saves having to look at the text and add slashes manually (as this works on an ad input by an operator).

Any ideas?

1
Contributor
2
Replies
4
Views
6 Years
Discussion Span
Last Post by tiggsy
0

I think i've solved this myself. Pretty obvious when you think about it.

I replaced the 2 lines I originally had:

$query = preg_replace('/\<a href=\"(.*)\"\>\<img height=\"(\d+)\" border=\"0\" width=\"(\d+)\" alt=\"(.*)\" src=\"(.*)\" \/\>\<\/a\>/',"INSERT INTO newproducts (productName,supplierName,productLink,imageLink,groupCode,currency,price,updated) VALUES ('$4','$supplier','$1','$5',",$ad);
$query .= "'$group','\$',$price,CURRENT_DATE)";

with:

preg_match ('/\<a href=\"(.*)\"\>\<img height=\"(\d+)\" border=\"0\" width=\"(\d+)\" alt=\"(.*)\" src=\"(.*)\" \/\>\<\/a\>/',$ad);
$productName = addslashes($matches[4]);
$query = "INSERT INTO newproducts (productName,supplierName,productLink,imageLink,groupCode,currency,price,updated) VALUES ('$productName','$supplier','{$matches[1]}','{$matches[5]}','$group','\$',$price,CURRENT_DATE)";

Edited by tiggsy: n/a

0

OK. There's an error in my "solved" code, so I thought I'd mod it here, in case anybody ever needs it:

preg_match ('/\<a href=\"(.*)\"\>\<img height=\"(\d+)\" border=\"0\" width=\"(\d+)\" alt=\"(.*)\" src=\"(.*)\" \/\>\<\/a\>/',$ad,$matches);
$productName = addslashes($matches[4]);
$query = "INSERT INTO newproducts (productName,supplierName,productLink,imageLink,groupCode,currency,price,updated) VALUES ('$productName','$supplier','{$matches[1]}','{$matches[5]}','$group','\$',$price,CURRENT_DATE)";
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.