English 中文(简体)
采用代号修改从实验室白空间开始的阵列
原标题:Using foreach to modify arrays starting with whitespace in php
  • 时间:2012-04-03 21:45:51
  •  标签:
  • php

第一次来到这里,新鲜发展。

试图修改具有多个线的文字档案的产出,有些则从特性开始,有些则带有白色空间/表格。 我想有一条从禁忌/白天空开始的路线,与直接摆在它面前的任何一条线连接起来。 我先看一下这样做,但我不敢确定如何加以安排。 我认为,我需要利用海滩检查每一条线,发现每一条有白色空间,并使用一系列reg子加入前线。 这正是我亲爱的,以及我应该到哪里来。

<?php

$file_handle = fopen("parse.txt", "r");

while (!feof($file_handle)){

    $each_line = fgets($file_handle);
    foreach ($each_line    ){

        }

    print $each_line . "<br />";
}

fclose($file_handle);

?>

The specific text file is at http://krondorkrew.com/parse/parse.txt If you go there and look at the index of the /parse folder, I have my current build of this php, also.

我的下一步是,通过在第一线进行探索,将每一条线分别分为不同的阵列:但在每一行,我仍然想在找你非常多的人寻求帮助之前,设法与这个问题作斗争(但如果你有任何见解,我会 t倒)。

Thank you in advance for all help!

问题回答

下面是一条略微缺失的密码:startsWith WhitespaceOrtab功能(很容易书写,我就这样作一例)。 其余部分已经运作:

foreach (file( parse.txt , FILE_IGNORE_NEW_LINES) as $line) {
    if (!startsWithWhitespaceOrTab($line)) {
        echo "
";
    }
    echo $line;
}

See the PHP file< function and the

Use file_get_contents() to read the file into a single string. You can then use regex to match white space, or a new line and then white space, etc.

我将担任该职务,但我不敢肯定你会再次做些什么。 页: 1 http://gskinner.com/RegExr/。

You ll probably want something along these lines:

<?php

$file_handle = fopen("parse.txt", "r");

$key = null;
$arr = array();

while (!feof($file_handle)){

    $each_line = fgets($file_handle);

    if ($key && preg_match( /^s+/ , $each_line)) {
        $arr[$key] .=     . trim($each_line);
    } else if (preg_match( /^([a-zA-Z]+):(.*)$/ , $each_line, $m)) {
        $key = $m[1];
        $arr[$key] = trim($m[2]);
    }

}

fclose($file_handle);

?>

还有一个解决办法。 • 为你们收集阵列中的线索。 我认为,我喜欢@pinouchon。

$file_handle = fopen("parse.txt", "r");
$fixed = array();
while (!feof($file_handle)){
    $line = fgets($file_handle);
    if (ctype_space($line[0])) { // Checks for whitespace
        $fixed[count($fixed)-1].= trim($line); // Adds to previous line
    } else {
        $fixed[] = trim($line); // Otherwise, keep line as is
    }
}
fclose($file_handle);

// Print
echo implode("
",$fixed); 




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...