简体   繁体   English

正则表达式php特殊字符

[英]regex php special characters

$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
    }
}

okay i have a list.text and a fleetyfleet.txt both can be viewed here i didn't post them for space sake http://pastebin.com/7hWDUG1b but what i want to do is find the words in list.txt but only add them to array if there prefix is Dimension Name but the special characters make it kinda tough I'm not sure what i should do 好吧,我有一个list.text和Fletyfleet.txt都可以在这里查看。我出于空间原因没有张贴它们http://pastebin.com/7hWDUG1b,但是我想做的就是在list.txt中找到单词,但是仅当它们的前缀为Dimension. Name时才将它们添加到数组中,但特殊字符使其很难使用,我不确定该怎么做

I had a similar problem where I needed to remove all non-ascii characters from a file. 我遇到了类似的问题,需要从文件中删除所有非ASCII字符。 Here's the regex I used: 这是我使用的正则表达式:

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

If you're on linux, here's a quick one-liner: 如果您使用的是Linux,那么这里有一个简单的方法:

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

Having to guess a little bit as I can't see the files from behind my firewall. 不得不猜测一点,因为我看不到来自防火墙后面的文件。 The following code might help you: 以下代码可能会帮助您:

<?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";
    }
  }
}
?>

This prints out lines that include a pattern DimensionName followed by 5 "anything" followed by whatever word was read from the first file file1.txt . 这将打印出包含以下格式的行: DimensionName模式,后跟5个“ anything”,后跟从第一个文件file1.txt读取的任何单词。

If you need this to be further refined, please leave a comment. 如果需要进一步完善,请发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM