2017-12-13 3 views
1

저는 PHP을 처음 접해서 유형 생성기를 만들려고합니다. 다른 배열 값을 하드 코딩하기 시작했습니다. 그래서 "()"사이의 모든 것이 텍스트 파일에 저장됩니다. 그러나 나는 내가 어떻게해야하는지 알 수 없다.배열을 텍스트 파일에서 읽는 방법?

Ex. $num = array (0,1,2,3,4,5,6,7,8,9); 숫자 0-9 대신 배열을 텍스트 파일에서 가져 오려고합니다.

<?php 
    $pass = array(); 
    $verb = array ('Klappa', 'Springande','Bakande', 'Badande', 
    'Cyklande', 'Jagande', 'Skrattande', 'Flygande', 'Simmande','Gissande'); 
    $num = array (0,1,2,3,4,5,6,7,8,9); 
    $sub = array ('katt', 'hund','fisk', 'padda', 'lama', 'tiger','panda', 'lejon', 'djur', 'telefon'); 
    $spec = array ('!','#','%','&','?'); 
    $pass[] = $verb[array_rand($verb)]; 
    for($i=0;$i<1;$i++){ 
     $pass[] = $num[array_rand($num)]; 
    } 
    $pass[] = $sub[array_rand($sub)]; 
    for($i=0;$i<1;$i++){ 
     $pass[] = $spec[array_rand($spec)]; 
    } 
    //shuffle($pass); 
    foreach($pass as $p){ 
     $password .= $p; 
    } 
    // echo "$password <br>"; 
?> 

내가 ('!','#','%','&','?');이 코드에 표시도 텍스트 파일에서 읽을 수 싶지 않아 : $spec = array ('!','#','%','&','?'); 여기

와 같은 내가 지금했던 방법이다. 어떻게해야합니까?

+1

텍스트 파일에서 읽은 부분이 보이지 않습니다. – ArtOsi

답변

0

당신이 할 수있는 일은 파일을 만들고 한 줄씩 원하는 문자를 삽입하는 것입니다. 당신은 배열로 값을 파일을 읽을 삽입이 코드를 사용할 수 있습니다

<?php 
    $handle = @fopen("/file.txt", "r"); 
    //declare your array here 
    if ($handle) { 
     while (($buffer = fgets($handle, 4096)) !== false) { 
      //add $buffer to your array. 
     } 
     fclose($handle); 
    } 
?> 

당신은 this as a reference보고 할 수 있습니다.

1

파일로 작성하는 경우, 당신은 할 수 : 출력은 다음과 같은 형식으로는 TextFile.txt하는 것

<?php 
    foreach(range(0,9) as $number){ 
     $output .= $number . PHP_EOL; 
    } 

    file_put_contents('textfile.txt', $output); 

?> 

:

0 
1 
2 
3 
4 
5 
6 
7 
8 
9 

가 배열로 그 다시 읽고, 당신은 다음

<?php 

    $input = file_get_contents('textfile.txt'); 

    $num = []; 
    $num = explode(PHP_EOL,$input); 

    //Take the blank element off the end of the array 
    array_pop($num); 

    echo '<pre>'; 
     print_r($num); 
    echo '</pre>'; 

?> 

당신에게 출력을 제공 할 수있는

,691,363 (210)
Array 
(
    [0] => 0 
    [1] => 1 
    [2] => 2 
    [3] => 3 
    [4] => 4 
    [5] => 5 
    [6] => 6 
    [7] => 7 
    [8] => 8 
    [9] => 9 
) 

당신이 그때 그렇게 영업 이익은 무엇을 볼 수있는 간단한 방법이 있습니다 알고 있어요,하지만 이런 식으로 일을 당신에게


을 줄 것이다

<?php 

    foreach($num as $number){ 
     //You can do whatever you want here, but i'm just going to print number 
     echo $number; 
    } 

?> 

를 호출 할 수있는 숫자를 읽어 계속.

관련 문제