如何使用php获取文本文件中的某些字符串

时间:2014-07-24 05:26:44

标签: php

我想在文本中回显/获取某些字符串,如: 该文件是test.txt 文件内容字符串是"数据。我' d。 1。在" 如何回显/只获取没有' 1'或者'我' d'不是其他字符串...... 请帮帮我

3 个答案:

答案 0 :(得分:1)

试试这个..

<?php
$data = file_get_contents("input.txt"); //read the file
$convert = explode("\n", $data); //create array separate by new line

for ($i=0;$i<count($convert);$i++)  
{
echo $convert[$i].', '; //write value by index
}
?>

输出:

数据,我,1,in,

或者如果它是某个字符串..

<?php
$data = file_get_contents("input.txt"); //read the file
$convert = explode("\n", $data); //create array separate by new line

for ($i=0;$i<count($convert);$i++)  
{
echo $convert[$i]=='1'. ; //write value by index
}
?>

输出:

1

答案 1 :(得分:0)

<?php

$file= file_get_contents("test.txt"); 
$convert = explode("\n", $file); `

for ($i=0;$i<count($convert);$i++)  
{

    if($i<count-1)///make your you doesn't have , in your last line
    {

        echo $convert[$i].', '; //it is writing the value`
    }
    else
    {

        echo $convert[$i];`

    }

}
?>

答案 2 :(得分:0)

$string = " data . I'd . 1 .in ";
// remove the spaces
$string = str_replace(' ', '',$string);
// convert to array
$words = explode('.', $string);

print $words[1]; // I'd
print $words[2]; // 1