正则表达式php特殊字符

时间:2014-02-26 22:21:37

标签: php regex

$word = file_get_contents('http://www.pixelmon-server-list.com/list.txt');
$content = file_get_contents('http://www.pixelmon-server-list.com/fleetyfleet.txt');
$found_dimensions = array(); // our array
$word_array = explode(' ', $word); // explode the list
foreach($word_array as $one_word) { // loop over it
    $str = 'DimensionName'.$one_word; // what are we looking for?
    if(strstr($content, $str) !== false) { // look for it!
        echo $one_word; // Just for demonstration purposes
        $found_dimensions[] = $one_word; // add to the array
    }
}

好吧,我有一个list.text和一个fleetyfleet.txt 这两个都可以在这里看到我没有张贴它们为了空间 http://pastebin.com/7hWDUG1b 但我想要做的是找到list.txt中的单词但只添加到数组中,如果前缀是Dimension Name 但是特殊字符使它有点难以理解我不知道应该做什么< / p>

2 个答案:

答案 0 :(得分:1)

我遇到了类似的问题,我需要从文件中删除所有非ascii字符。这是我使用的正则表达式:

s/[^\x00-\x7F]//g

如果您使用的是linux,那么这是一个快速的单行程序:

perl -p -i -e "s/[^\x00-\x7F]//g" list.txt

答案 1 :(得分:0)

不得不猜一点,因为我看不到防火墙后面的文件。以下代码可能对您有所帮助:

<?php
$f1 = explode("\n",file_get_contents("./file1.txt"));
$f2 = file_get_contents("./file2.txt");
$found = array();
foreach($f1 as $x) {
  $str = "/DimensionName.....$x./";
  if (strlen($x)>0) {
    if (preg_match($str, $f2, $matches)) {
      echo $matches[0]."\n";
    }
  }
}
?>

这将打印出包含模式DimensionName后跟5“任何内容”的行,后跟从第一个文件file1.txt中读取的任何单词。

如果您需要进一步完善,请发表评论。

相关问题