开发者

PHP String Explode Problem via Spacing

开发者 https://www.devze.com 2023-03-23 12:45 出处:网络
I am trying to do a very simple list explode using spaces as the delimiters. However, I am have some problems with th开发者_如何转开发e following string...

I am trying to do a very simple list explode using spaces as the delimiters. However, I am have some problems with th开发者_如何转开发e 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);


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


Would this work?

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消