How to judge an image url that if the link contains words ads
ad
, then pass insert into the database. Then it should be insert into first data, and pass the second one. Thanks,
PHP CODE
foreach($data['image'] as $item) {
$title = $item['title'];
$image = $item['image_url'];
mysql_query("SET NAMES utf8");
mysql_query("INSERT INTO article (title, image) VALUES ('".$title."', '".$image."')");
}
JSON TREE
{
"image": [
{
"title": "the big lake",
"image_url": "http://localhost/json/image/the_big_lake.jpg"
},
{
"title": "Nike Air",
"image_url": "http://localhost/json/image/12087689_ads.jpg"
}
]
开发者_开发百科}
I'll assume you want to insert all images that do NOT have "ad" in the image url... if you want ONLY ads, change the ===
in the if statement to a !==
. Make sure to keep it as either a triple-equals or exclamation-double-equals.
Also note that this is not a very reliable method - what if the image were called "my_dad_and_mom.jpg". It contains "ad", but is not an ad.
foreach($data['image'] as $item) {
if (strpos($item['image_url'], 'ad') === false) {
mysql_query("SET NAMES utf8"); // not sure why this is needed....
mysql_query("INSERT INTO article (title, image) VALUES ('".$item['title']."', '".$item['image_url']."')");
}
}
EDIT: This is sort of quick and dirty...
$forbidden_words = array(
'ads',
'ad',
'sex',
'xxx'
);
function str_in_array($str, $array) {
foreach ($array as $token) {
if (stristr($str, $token) !== FALSE)
return true;
}
return false;
}
foreach($data['image'] as $item) {
if (str_in_array($item['image_url'], $forbidden_words) === false) {
mysql_query("SET NAMES utf8"); // not sure why this is needed....
mysql_query("INSERT INTO article (title, image) VALUES ('".$item['title']."', '".$item['image_url']."')");
}
}
like this?
foreach($data['image'] as $item) {
$title = $item['title'];
$image = $item['image_url'];
if(preg_match('/ad/',$image){
mysql_query("SET NAMES utf8");
mysql_query("INSERT INTO article (title, image) VALUES ('".$title."', '".$image."')");
}
}
精彩评论