0

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);
3
  • /n? Did you mean \n? What happens instead? Are you sure you opened and read the file correctly? Commented Jul 28, 2011 at 23:16
  • 1
    that service serves up 1 "" enclosed dataset per line, so using fgets to get data a line at a time would be simpler than exploding it Commented Jul 28, 2011 at 23:27
  • Sorry about that typo, I was using backslashes for special string characters not forwardslashes. Commented Jul 29, 2011 at 4:23

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

Would this work?

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

2 Comments

Unless, you mean to say that there aren't double quotes as part of the delimiter. Can you show us the exact, literal string you're trying to explode? And show us how the array should properly look?
$newArr = explode('" "', substr($quote,1,-1));
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.