0

I'm no php expert... so forgive me if I'm overlooking something...

I've got a csv file with two columns, looks like this:

 john, VSgv4lEpuGS0vHIKapJZV7o...
 jane, yHKy6NW6YJZzloFhLDQUJIN...

the first with simple names, the second with a 1000+ character string. I'm trying to echo these out into a page where the simple name becomes a hyperlink with the long character string as its source. I've got the following:

    <?php

            if (($list = fopen("list.csv", "r")) !== FALSE) {  
                while (($data = fgetcsv($list, 1000, ",")) !== FALSE) {
                    echo "<div><a href='localhost:8888/folder/".$data[1]."'>" . $data[0] . "</a></div>"; 
                }
                fclose($list);
            }

    ?>

what happens instead is that the name shows up as a link, but the href contains everything but the last 30+ characters of the long string. And the strangest part is that those remaining 30+ characters show up below that div... within its own within its own (ie. imitating the structure I set up for the linked name)

...any ideas why this might be happening && how to fix it ???? ...I'm pretty stumped.

4

3 回答 3

3

My first guess would be that in your very long character string you have a '<' or '>' character somewhere which breaks the html. You could try wrapping $data[1] in the function htmlspecialchars() which escapes these characters..

Documentation:
http://php.net/manual/en/function.htmlspecialchars.php

Example:

echo "<div><a href='localhost:8888/folder/".htmlspecialchars( $data[1] )."'>" . htmlspecialchars( $data[0] ) . "</a></div>"; 
于 2013-05-17T20:38:05.263 回答
1

The line here:

while (($data = fgetcsv($list, 1000, ",")) !== FALSE) {

Reads a thousand characters from the file. and then processes it. If a field is over that length, it'll process what it can, and then carry on with the rest of the field on the next iteration of the while loop.

You might be better off using fgets to read everything into a variable and processing it; or possibly just increasing the number to more than 1000 as a short term fix.

于 2013-05-17T20:35:40.720 回答
0

Citing the documentation concerning the second parameter (length) of fgetcsv():

Must be greater than the longest line (in characters) to be found in the CSV file (allowing for trailing line-end characters).

So have a look, how long your longest line is, and adjust the parameter accordingly. If the second entry alone is longer then a thousand characters, the parameter has to be even larger.

于 2013-05-17T20:36:56.143 回答