Php 从txt文件中获取特定数据

Php 从txt文件中获取特定数据,php,Php,我有txt文件,我需要在一个循环中回显特定的数据 假设我的txt文件名是:myfile.txt 里面的结构是这样的: etc="Orange" src="stack1" etc="Blue" src="stack2" etc="Green" src="stack3" etc="Red" src="stack4" 如何在PHP中回显这些值:橙色、蓝色、绿色、红色?您可以使用preg\u match\u all来实现: <?php # get your text $txt =

我有txt文件,我需要在一个循环中回显特定的数据

假设我的txt文件名是:myfile.txt

里面的结构是这样的:

etc="Orange" src="stack1"
etc="Blue" src="stack2"
etc="Green" src="stack3"
etc="Red" src="stack4"

如何在PHP中回显这些值:橙色、蓝色、绿色、红色?

您可以使用
preg\u match\u all
来实现:

<?php
    # get your text
    $txt = file_get_contents('your_text_file.txt');

    # match against etc="" (gets val inside the quotes)
    preg_match_all('/etc="([^"]+)"/', $txt, $matches);

    # actual values = $matches[1]
    $values = $matches[1];

    echo '<pre>'. print_r($values, 1) .'</pre>';

您可以使用
preg\u match\u all
进行以下操作:

<?php
    # get your text
    $txt = file_get_contents('your_text_file.txt');

    # match against etc="" (gets val inside the quotes)
    preg_match_all('/etc="([^"]+)"/', $txt, $matches);

    # actual values = $matches[1]
    $values = $matches[1];

    echo '<pre>'. print_r($values, 1) .'</pre>';

您可以检索文件中的内容。
之后,您需要检查
file\u get\u content
是否返回了错误代码(
false
) 将使用RegExp仅筛选出您需要的内容。特别是:


<?php

$fichero = file_get_contents('./myfile.txt', false);

if($fichero === false){ //if file_get_contents() return false, the file isn't found, if its found, return data.
    echo "Can't find file.\n";
}else{ //If file is find, this condition is executed.
    $output = array(); //this variable is who will get the output of regular expression pattern from next line function.
    preg_match_all('/([A-Z])\w+/',$fichero, $output);
    for($i = 0; $i < count($output[0]); $i++){ //Iterate throught the first array inside of array of $output, count(array) is for get length of array.

        echo $output[0][$i]; //Print values from array $output[0][$i]
        if($i + 1 != count($output[0])){ //if not equal to length of array, add , at end of printed value of output[0][$i]
            echo ', ';
        }else{ //if equal to length of array, add . at end of printed value of $output[0][$i]
            echo '.';
        }

    }
}

?>

所有匹配项都收集在
$matches
数组中(先前定义了
$matches
并不需要)

最后,您需要将收集到的值转换为字符串,您可以使用函数来实现这一点

您可以检索文件中的内容。
之后,您需要检查
file\u get\u content
是否返回了错误代码(
false
) 将使用RegExp仅筛选出您需要的内容。特别是:


<?php

$fichero = file_get_contents('./myfile.txt', false);

if($fichero === false){ //if file_get_contents() return false, the file isn't found, if its found, return data.
    echo "Can't find file.\n";
}else{ //If file is find, this condition is executed.
    $output = array(); //this variable is who will get the output of regular expression pattern from next line function.
    preg_match_all('/([A-Z])\w+/',$fichero, $output);
    for($i = 0; $i < count($output[0]); $i++){ //Iterate throught the first array inside of array of $output, count(array) is for get length of array.

        echo $output[0][$i]; //Print values from array $output[0][$i]
        if($i + 1 != count($output[0])){ //if not equal to length of array, add , at end of printed value of output[0][$i]
            echo ', ';
        }else{ //if equal to length of array, add . at end of printed value of $output[0][$i]
            echo '.';
        }

    }
}

?>

所有匹配项都收集在
$matches
数组中(先前定义了
$matches
并不需要)


最后,您需要将收集到的值转换为字符串,您可以使用函数来实现这一点。

I exaplane all on code
//comments



我检查了所有代码
//注释



有了拆分和数组,你就可以得到它了。您好。你能用一个代码作为答案告诉我吗?javascript的一个例子就是这个答案,但好的,我能做到。试试打印(explode(“”),file_get_contents('your_text_file.txt'))看看你会得到什么。使用拆分和数组你可以得到它。你好。你能用一个代码作为答案告诉我吗?javascript的一个例子可以是这个答案,但好的,我可以做到。试试类似于print_r(explode(“”,file_get_contents('your_text_file.txt'))的东西看看你会得到什么。SO的目的是帮助人们修复他们的代码。而不是为他们编写代码。@MilanG Right,我们没有被雇佣呵呵。SO的目的是帮助人们修复他们的代码。而不是为他们编写代码。@MilanG Right,我们没有被雇佣呵呵。