Jump to content

String Streams In Php


boen_robot
 Share

Recommended Posts

I've been recently writing a new teplating engine for myself... among one of the requirements I set to myself is to work equally well with files, and just-in-time strings. I've done a little with C++'s fstream, cin/cout/err and stringstream objects, and I know for a fact PHP has equivalents for the first two, which are readable and writable with fread() and fwrite() respectively. However, I just reached a road block on the third... is there a way I could open an empty stream, without associating it with a file (temporary or otherwise), or in other words, could I create a string stream? If so, how? I expect to use quite a few such streams if possible, so using temporary files will leave the OS really unclean.[edit]Well, I found the tmpfile() funciton... but that would still create a file (even though a temporary one)... is there a better way?[/edit]

Edited by boen_robot
Link to comment
Share on other sites

It looks like C++'s stringstream is just a wrapper to work with a string as if it were a stream, what differences are you expecting in PHP between a string as a stream vs. just using a string in memory? Are you just looking for a wrapper like with C++ where you have code that works on streams and you want to use the same code for a regular string?Look into opening a stream using the php://memory or php://temp wrappers:http://www.php.net/manual/en/wrappers.php.phpAlso the data wrapper:http://www.php.net/manual/en/wrappers.data.php

Edited by justsomeguy
Link to comment
Share on other sites

Are you just looking for a wrapper like with C++ where you have code that works on streams and you want to use the same code for a regular string?
Exactly. I have a piece of code that works with streams, and I want to use it within another piece of code. The other function will give the stream function a string stream, and will then post-process the resulting string stream.php://temp suits the situation perfectly. Thanks. Edited by boen_robot
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...