treat a string like a file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paladaxar
Forum Commoner
Posts: 85
Joined: Fri Jun 18, 2004 11:50 pm

treat a string like a file

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: treat a string like a file

Post 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
paladaxar
Forum Commoner
Posts: 85
Joined: Fri Jun 18, 2004 11:50 pm

Re: treat a string like a file

Post 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
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: treat a string like a file

Post by Eran »

Check out the comments section in the documentation page I linked you to, there are some custom implementations of csv parsing functions.
Post Reply