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?

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

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)";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.