Page 1 of 1

treat a string like a file

Posted: Wed Jul 02, 2008 6:59 pm
by paladaxar
Is there any way to treat a string like a file in PHP?

For example...I have a string: $str = "Hi, I'm a string in php";

and I have a file "hi.txt" and its contents are "Hi, I'm a file in php".

Now with hi.txt, I can do all kinds of file operations like fgetcsv().

Is there any way that I can do these operations on the string? Sort of trick PHP into thinking that $str is actually a file that I can get a $handle on.

Re: treat a string like a file

Posted: Wed Jul 02, 2008 7:27 pm
by Eran
Either save the string as a file or use string methods to emulate the file methods available in php. I find it hard to see why you would want to do that though, aside from fgetcsv - which has its string equivalent http://www.php.net/manual/en/function.str-getcsv.php

Re: treat a string like a file

Posted: Wed Jul 02, 2008 8:01 pm
by paladaxar
The server that I am on cannot write to the file system, so everything has to be saved to the db.

I tried the str_getcsv() but I got "Call to undefined function str_getcsv()"

I'm running PHP 5.1.2

Re: treat a string like a file

Posted: Wed Jul 02, 2008 8:23 pm
by Eran
Check out the comments section in the documentation page I linked you to, there are some custom implementations of csv parsing functions.