-1

さまざまな URL を含む txt ファイルがあります。リストを解析し、いくつかの URL をスキップして、最終的なクリーン リストを取得したいと考えています。以下のリストの一部を参照してください。

http://www.example.com/example1/
http://www.example.com/example2/
http://www.example.com/example3/
http://www.example.com/example4/
http://www.example.com/example.js
http://www.example.com/example.css
http://www.example.com/example1.js?v=123
http://www.example.com/{path}
http://www.example.com/feed/
http://www.example.com/?p=66

.jsまたは.cssまたは{path}または/feed/または?p=66のようなすべての URL をスキップして、すべてを txt ファイルに再度出力したいと考えています。私はPHPを使ってそれをやりたいです。何かアドバイス ?

4

1 に答える 1

1
<?php 

  $list = "http://www.example.com/example1/
http://www.example.com/example2/
http://www.example.com/example3/
http://www.example.com/example4/
http://www.example.com/example.js
http://www.example.com/example.css
http://www.example.com/example1.js?v=123
http://www.example.com/{path}
http://www.example.com/feed/
http://www.example.com/?p=66";

  $arr = preg_split("/[\r\n]+/",$list);

  // check our input array
  print_r($arr);

  $map = array();
  foreach($arr as $v){
    if(!preg_match("/({path}|\.(js|css)|\?p=\d+|\/feed\/)$/",$v)){
      $map[] = $v;
    }
  };

  // check our output array
  print_r($map);

?>

{path}これは、または.cssまたは.jsまたは?p=##(# は数字) または で終わらない URL に一致させることを前提としています/feed//example1.js?v=123これが、一致するものが依然として存在する理由です。末尾だけでなく、文字列の任意の場所に一致させるに$は、正規表現の末尾 (単語 の直後feed) から を削除します。

私のコンソール出力:

Array
(
    [0] => http://www.example.com/example1/
    [1] => http://www.example.com/example2/
    [2] => http://www.example.com/example3/
    [3] => http://www.example.com/example4/
    [4] => http://www.example.com/example.js
    [5] => http://www.example.com/example.css
    [6] => http://www.example.com/example1.js?v=123
    [7] => http://www.example.com/{path}
    [8] => http://www.example.com/feed/
    [9] => http://www.example.com/?p=66
)
Array
(
    [0] => http://www.example.com/example1/
    [1] => http://www.example.com/example2/
    [2] => http://www.example.com/example3/
    [3] => http://www.example.com/example4/
    [4] => http://www.example.com/example1.js?v=123
)
于 2013-02-20T14:19:17.480 に答える