2016-05-10 9 views
0

私は配列内にドメインのリストを持っています。PHP/PDO - URLに特定のドメインのリストが含まれているかどうかをチェックします。

$notAllowedWebsites = array('google.com','yahoo.com','facebook.com'); 

ここでは、URLがいっぱいの列を含むMySQLデータベースがあります。

  1. facebook.com/testpage
  2. google.com/search
  3. ft.com/big-success bbc.co.uk/news/going-to-the-sea
  4. google.com/time

ここで、ドメインにURLが存在する上記の表から上記のURLを抽出します。私が抽出したい言い換えれば: 1. facebook.com/testpage 2. google.com/search 3. google.com/time

これは私のコードです:

foreach($notAllowedWebsite as $notAllowedWebsite){ 
    $sqlQueryBusinessWebsite = $dbh->prepare("SELECT business_website FROM client_table WHERE business_website = :business_website"); 
    $sqlQueryBusinessWebsite->execute(array(':business_website'=>$notAllowedWebsite)); 
    $sqlResultBusinessWebsite = $sqlQueryBusinessWebsite->fetch(); 
    $businessWebsiteDB = $sqlResultBusinessWebsite['business_website']; 
} 

私は」この行をどのように変更するのかわからない場合は、$sqlQueryBusinessWebsite = $dbh->prepare("SELECT business_website FROM client_table WHERE business_website = :business_website");ドメインがURLに存在するかどうかを確認してください。

ありがとうございました。

+0

LIKE句を試しましたか? –

+0

私は上記の私のPDOコードで実装する方法がわかりません – user6043723

+0

foreachループを使用しないでIN句を使用します。acheck http://stackoverflow.com/questions/920353/can-i-bind-an-array-to-an -in-condition – Saty

答えて

0

は、LIKE句を使用します。Regular Expressionを使用して

foreach($notAllowedWebsite as $notAllowedWebsite){ 
    $sqlQueryBusinessWebsite = $dbh->prepare("SELECT business_website FROM client_table WHERE business_website LIKE :business_website"); 
    $sqlQueryBusinessWebsite->execute(array(':business_website'=>'%'.$notAllowedWebsite.'%')); 
    $sqlResultBusinessWebsite = $sqlQueryBusinessWebsite->fetch(); 
    $businessWebsiteDB = $sqlResultBusinessWebsite['business_website']; 
} 
+0

あなたのコードを試しましたが、IDは何も返されませんでした – user6043723

+0

あなたはgoogle.com、yahoo.comで始まるデータベースの値は本当ですか?何も前に? –

+0

はい、そうです。ありがとうございます。 – user6043723

0

は、私はクリーンな方法があります確信しているが、以下が含まれているすべてのサイトを取得します

SELECT business_website FROM business_website WHERE business_website RLIKE '(?=.*facebook)(?=.*yahoo)' 
+0

私は何百ものURLを持っているので、私は配列を使用しています。 – user6043723

0

ここに最善の解決策になります禁止リストのドメイン:

CREATE TABLE `banned_sites` (
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, 
    `domain` VARCHAR(255) NOT NULL DEFAULT '0', 
    PRIMARY KEY (`id`) 
); 

CREATE TABLE `entries` (
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, 
    `website` VARCHAR(255) NOT NULL DEFAULT '0', 
    PRIMARY KEY (`id`) 
); 

insert into banned_sites (domain) values ('facebook.com'), ('yahoo.com'), ('google.com'); 
insert into entries (website) values ('http://www.facebook.com/thisisatest'), ('http://www.msn.com'), ('http://google.com'), ('yahoo.com'); 

次に行うことができます:

select e.* from entries e inner join banned_sites bs on (e.website like concat('%', concat(bs.domain, '%'))) 

SQLFiddleは現時点では機能していません。そうでない場合は、動作していることを示します。

関連する問題