PHP 字符串因间距而爆炸的问题

发布于 2024-11-27 00:48:09 字数 703 浏览 5 评论 0原文

我正在尝试使用空格作为分隔符来创建一个非常简单的列表爆炸。但是,我对以下字符串有一些问题...

"+0.59 - +0.58%" "+0.06 - +0.14%" "-0.47 - -1.07%" "-0.77 - -0.20%" //输入

和结果数组应该由每个空格分隔(引号也被删除)

Array ( [0] => +0.59 [1] => - [2] => +0.58% +0.06 [3] => - [4] => +0.14% -0.47 [5] => -1.07% -0.77 [7] => - [8] => -0.20% )

基本上空格未被正确识别。我已经尝试通过 /n /r 和 '/\s*/m' 来分隔它。

这是我的代码片段。

$open = fopen("http://finance.yahoo.com/d/quotes.csv?s=$ticker&f=c&e=.csv", "r");
$quote = fread($open, 2000);
fclose($open);
$quote = explode(" ", $quote);
foreach ($quote as &$value) {
     $value = str_replace('"',"",$value);
}
//print_r($tickerlist);
print_r($quote);

I am trying to do a very simple list explode using spaces as the delimiters. However, I am have some problems with the following string...

"+0.59 - +0.58%" "+0.06 - +0.14%" "-0.47 - -1.07%" "-0.77 - -0.20%" //Input

And the resultant array which is supposed to be separated by each space (quotes also removed)

Array ( [0] => +0.59 [1] => - [2] => +0.58% +0.06 [3] => - [4] => +0.14% -0.47 [5] => - [6] => -1.07% -0.77 [7] => - [8] => -0.20% )

Basically the spaces aren't being recognized correctly. I have already tried separating it via /n /r and '/\s*/m'.

Here is a snippet of my code.

$open = fopen("http://finance.yahoo.com/d/quotes.csv?s=$ticker&f=c&e=.csv", "r");
$quote = fread($open, 2000);
fclose($open);
$quote = explode(" ", $quote);
foreach ($quote as &$value) {
     $value = str_replace('"',"",$value);
}
//print_r($tickerlist);
print_r($quote);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆光飞翔i 2024-12-04 00:48:09

在适当的编辑器中打开一个文件(vim 会很适合这个。也许是 notepad++)并检查 tab 字符和 \r\n

Open a file in a proper editor (vim would be nice for this. maybe notepad++) and check for tab character and \r and \n.

那一片橙海, 2024-12-04 00:48:09

这行得通吗?

$newArr = explode('" "', $quote);

Would this work?

$newArr = explode('" "', $quote);
我不是你的备胎 2024-12-04 00:48:09

如果要转换数据,只需使用此函数将 csv 文件解析为数组即可。

function csv2array($string, $separatorChar = ',', $enclosureChar = '"', $newlineChar = "\n") 
{
    $array = array();
    $size = strlen($string);
    $columnIndex = 0;
    $rowIndex = 0;
    $fieldValue = "";
    $isEnclosured = False;

    for($i=0; $i<$size;$i++) 
    {
        $char = $string{$i};
        $addChar = "";

        if($isEnclosured) 
        {
            if($char == $enclosureChar) 
            {
                if($i+1<$size && $string{$i+1} == $enclosureChar)
                {
                    $addChar = $char;
                    $i++; 
                }
                else
                {
                    $isEnclosured = false;
                }
            }
            else 
            {
                $addChar=$char;
            }
        }
        else 
        {
            if($char==$enclosureChar) 
            {
                $isEnclosured = true;
            }
            else 
            {
                if($char==$separatorChar) 
                {
                    $array[$rowIndex][$columnIndex] = $fieldValue;
                    $fieldValue = "";
                    $columnIndex++;
                }
                elseif($char==$newlineChar) 
                {
                    //echo $char;
                    $array[$rowIndex][$columnIndex] = $fieldValue;
                    $fieldValue="";
                    $columnIndex=0;
                    $rowIndex++;
                }
                else 
                {
                    $addChar=$char;
                }
            }
        }

        if($addChar != "")
        {
            $fieldValue.=$addChar;
        }
    }

    if($fieldValue) 
    {
        $array[$rowIndex][$columnIndex] = $fieldValue;
    }

    return $array;
}

这只会解析关联形式的所有内容并返回该数组。

If you want to convert the data, you can simply use this function to parse the csv file into an array.

function csv2array($string, $separatorChar = ',', $enclosureChar = '"', $newlineChar = "\n") 
{
    $array = array();
    $size = strlen($string);
    $columnIndex = 0;
    $rowIndex = 0;
    $fieldValue = "";
    $isEnclosured = False;

    for($i=0; $i<$size;$i++) 
    {
        $char = $string{$i};
        $addChar = "";

        if($isEnclosured) 
        {
            if($char == $enclosureChar) 
            {
                if($i+1<$size && $string{$i+1} == $enclosureChar)
                {
                    $addChar = $char;
                    $i++; 
                }
                else
                {
                    $isEnclosured = false;
                }
            }
            else 
            {
                $addChar=$char;
            }
        }
        else 
        {
            if($char==$enclosureChar) 
            {
                $isEnclosured = true;
            }
            else 
            {
                if($char==$separatorChar) 
                {
                    $array[$rowIndex][$columnIndex] = $fieldValue;
                    $fieldValue = "";
                    $columnIndex++;
                }
                elseif($char==$newlineChar) 
                {
                    //echo $char;
                    $array[$rowIndex][$columnIndex] = $fieldValue;
                    $fieldValue="";
                    $columnIndex=0;
                    $rowIndex++;
                }
                else 
                {
                    $addChar=$char;
                }
            }
        }

        if($addChar != "")
        {
            $fieldValue.=$addChar;
        }
    }

    if($fieldValue) 
    {
        $array[$rowIndex][$columnIndex] = $fieldValue;
    }

    return $array;
}

this will just parse everything in its associative form and return you that array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文