【问题标题】:Using a list with regex in php在 php 中使用带有正则表达式的列表
【发布时间】:2014-02-26 21:02:06
【问题描述】:
$word = file('list.txt');
$content = file_get_contents('fleetyfleet.txt');

fleetyfleet.txt 包含

DimensionNameGreenrandomjunktextextcstuffidonwantDimensionNameBluemoreeecrapidontwannaseeeDimensionNameYellowDimensionNameGrayrandomcrapDimensionNameRed

list.txt 包含

绿蓝黄

我想要的是能够找到 DimensionName 然后如果单词在 list.txt 中,则将其后的单词存储到数组中

所以当脚本运行时,存储在数组中的结果将是绿色、蓝色、黄色,但不是红色和灰色,因为它们不在我们的列表中

【问题讨论】:

    标签: php regex


    【解决方案1】:

    展开你的单词列表,然后循环遍历它:

    $word = file_get_contents('list.txt');
    $content = file_get_contents('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 "Found $one_word<br/>"; // Just for demonstration purposes
            $found_dimensions[] = $one_word; // add to the array
        }
    }
    

    【讨论】:

    • 我无法让它正常工作 pixelmon-server-list.com/list.txt'); $content = file_get_contents('pixelmon-server-list.com/fleetyfleet.txt'); $found_dimensions = 数组(); // 我们的数组 $word_array = explode(' ', $word); // 分解列表 foreach($word_array as $one_word) { // 循环它 $str = 'DimensionName'.$one_word; // 我们在找什么? if(strstr($content, $str) !== false) { // 寻找它!回声 $one_word; // 仅用于演示目的 $found_dimensions[] = $one_word; // 添加到数组中 } } ?>
    • 你能解释一下什么不起作用吗?如果pixelmon-server-list.com指的是远程服务器,每次使用都需要在前面加上http://。
    • http 在我的上面,当我发布它时它刚刚删除,但它没有回应结果,因为我知道它应该找到一些但没有回应
    • 根据问题中的信息,这对我来说很好。 list.txt 中单词之间的分隔符是什么?
    • 我有一个错字;第一行中的file 应该是file_get_contents。
    【解决方案2】:

    诀窍是通过分隔符explode() 您的文件内容,然后使用列表中的单词过滤结果。 $matching 包含所有找到的片段匹配列表。

    $words = file_get_contents('list.txt');
    $text = file_get_contents('content.txt');
    
    $elements = explode('DimensionName', $text); // trick
    $words = explode(' ', $words); // words list
    
    // leverage native PHP function
    $matching = array_reduce($elements, function($result, $item) use($words) {
        if(in_array($item, $words)) { $result[] = $item; }
        return $result;
        }, array());
    
    var_dump($matching);
    

    更新

    array_reduce() 是一个简洁的函数,但我完全忘记了一种更简单的解决方案:

    $matching = array_intersect($words, $elements);
    

    来自docs:

    array_intersect() 返回一个数组,其中包含所有参数中存在的 array1 的所有值。请注意,密钥是保留的。

    所以$matching 将包含$words 数组中存在于$elements 中的所有元素。这是这里最简单也可能是最好的解决方案。

    【讨论】:

    • 我认为您会更喜欢我更新后的答案。 :)
    【解决方案3】:

    我认为这就是你想要的。我遍历list.txt 中的每个单词,然后使用preg_match() 来查看该维度是否在fleetyfleet.txt 中(对于我的示例,您不需要正则表达式,但我留下了它以防你的例子更复杂)。如果匹配,则将匹配项添加到您的 $dimensions 数组中。

    <?php
    $dimensions = array();
    
    $content = 'DimensionNameGreenrandomjunktextextcstuffidonwantDimensionNameBluemoreeecrapidontwannaseeeDimensionNameYellowDimensionNameGrayrandomcrapDimensionNameRed';
    $list = 'Green Blue Yellow';
    
    foreach(explode(' ', $list) as $word) {
        if(preg_match('/DimensionName' . trim($word) . '/', $content, $matches) {
            $dimensions[] = reset($matches);
        }
    }
    
    var_dump($dimensions);
    // array(3) {
    //     [0]=> string(18) "DimensionNameGreen"
    //     [1]=> string(17) "DimensionNameBlue"
    //     [2]=> string(19) "DimensionNameYellow"
    // }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-19
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 1970-01-01
      相关资源
      最近更新 更多