how do I use php://memory?

how do I use php://memory?

am 30.01.2010 01:35:00 von Mari Masuda

Hello,

I have a function that uses tidy to attempt to clean up a bunch of =
crappy HTML that I inherited. In order to use tidy, I write the crappy =
HTML to a temporary file on disk, run tidy, and extract and return the =
clean(er) HTML. The program itself works fine but with all of the disk =
access, it runs quite slowly. I saw on this page =
(http://www.php.net/manual/en/wrappers.php.php) that I could write to =
memory by using php://memory. Unfortunately, I could not quite get it =
to work. The problem is that in the below function, the code within the =
[[[if (file_exists($dirty_file_path))]]] does not get run if I change =
[[[$dirty_file_path]]] to "php://memory". Has anyone ever successfully =
used php://memory before? If so, what can I do to use it in my code? =
Thank you.


//===================3D= 3D=====
==================== =====3D=
=========3D
function cleanUpHtml($dirty_html, $enclose_text=3Dtrue) {

$parent_dir =3D "/filesWrittenFromPHP/";
$now =3D time();
$random =3D rand();
=09
//save dirty html to a file so tidy can process it
$dirty_file_path =3D $parent_dir . "dirty" . $now . "-" . =
$random . ".txt";
$dirty_handle =3D fopen($dirty_file_path, "w");
fwrite($dirty_handle, $dirty_html);
fclose($dirty_handle);

$cleaned_html =3D "";
$start =3D 0;
$end =3D 0;

if (file_exists($dirty_file_path)) {
exec("/usr/local/bin/tidy -miq -wrap 0 -asxhtml =
--doctype strict --preserve-entities yes --css-prefix \"tidy\" =
--tidy-mark no --char-encoding utf8 --drop-proprietary-attributes yes =
--fix-uri yes " . ($enclose_text ? "--enclose-text yes " : "") . =
$dirty_file_path . " 2> /dev/null");

$tidied_html =3D file_get_contents($dirty_file_path);
=09
$start =3D strpos($tidied_html, "") + 6;
$end =3D strpos($tidied_html, "") - 1;
=09
$cleaned_html =3D trim(substr($tidied_html, $start, =
($end - $start)));
}
=09
unlink($dirty_file_path);


return $cleaned_html;
}
//===================3D= 3D=====
==================== =====3D=
=========3D


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: how do I use php://memory?

am 30.01.2010 01:38:28 von Nathan Nobbe

--001636e0a75d8731c7047e56f876
Content-Type: text/plain; charset=UTF-8

On Fri, Jan 29, 2010 at 5:35 PM, Mari Masuda wrote:

> Hello,
>
> I have a function that uses tidy to attempt to clean up a bunch of crappy
> HTML that I inherited. In order to use tidy, I write the crappy HTML to a
> temporary file on disk, run tidy, and extract and return the clean(er) HTML.
> The program itself works fine but with all of the disk access, it runs
> quite slowly.


why read from disk in the first place?

http://us3.php.net/manual/en/tidy.parsestring.php

-nathan

--001636e0a75d8731c7047e56f876--

Re: how do I use php://memory?

am 30.01.2010 01:48:07 von Jochem Maas

Op 1/30/10 1:35 AM, Mari Masuda schreef:
> Hello,
>
> I have a function that uses tidy to attempt to clean up a bunch of crappy HTML that I inherited. In order to use tidy, I write the crappy HTML to a temporary file on disk, run tidy, and extract and return the clean(er) HTML. The program itself works fine but with all of the disk access, it runs quite slowly. I saw on this page (http://www.php.net/manual/en/wrappers.php.php) that I could write to memory by using php://memory. Unfortunately, I could not quite get it to work. The problem is that in the below function, the code within the [[[if (file_exists($dirty_file_path))]]] does not get run if I change [[[$dirty_file_path]]] to "php://memory". Has anyone ever successfully used php://memory before? If so, what can I do to use it in my code? Thank you.

what does it matter that it runs slowly, run it once and be done with it?
alternatively use the php tidy extension and avoid the file system and shelling out altogether.

actually I'd imagine shelling out from a webserver process is the bottle neck and not saving/reading
from the file system.

lastly I don't suppose you've heard of /dev/shm ?

and, er, no, I don't have experience with php://memory but you might try searching for
other people's code:

http://www.google.com/codesearch?q=php%3A%2F%2Fmemory&hl=en& btnG=Search+Code

> //==========================================================
> function cleanUpHtml($dirty_html, $enclose_text=true) {
>
> $parent_dir = "/filesWrittenFromPHP/";
> $now = time();
> $random = rand();
>
> //save dirty html to a file so tidy can process it
> $dirty_file_path = $parent_dir . "dirty" . $now . "-" . $random . ".txt";
> $dirty_handle = fopen($dirty_file_path, "w");
> fwrite($dirty_handle, $dirty_html);
> fclose($dirty_handle);
>
> $cleaned_html = "";
> $start = 0;
> $end = 0;
>
> if (file_exists($dirty_file_path)) {
> exec("/usr/local/bin/tidy -miq -wrap 0 -asxhtml --doctype strict --preserve-entities yes --css-prefix \"tidy\" --tidy-mark no --char-encoding utf8 --drop-proprietary-attributes yes --fix-uri yes " . ($enclose_text ? "--enclose-text yes " : "") . $dirty_file_path . " 2> /dev/null");
>
> $tidied_html = file_get_contents($dirty_file_path);
>
> $start = strpos($tidied_html, "") + 6;
> $end = strpos($tidied_html, "") - 1;
>
> $cleaned_html = trim(substr($tidied_html, $start, ($end - $start)));
> }
>
> unlink($dirty_file_path);
>
>
> return $cleaned_html;
> }
> //==========================================================
>
>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: how do I use php://memory?

am 30.01.2010 02:00:27 von Shawn McKenzie

Mari Masuda wrote:

> Has anyone ever successfully used php://memory before? If so, what
> can I do to use it in my code? Thank you.

No, but I was intrigued to try it, so I tested this:

$text = 'Some text.';
file_put_contents('php://memory', $text);
echo file_get_contents('php://memory');

And it returned nothing. The docs suck on this and it apparently
doesn't work. I see others use it with fopen(), but there is no mention
of which file functions it works with and which it doesn't.

--
Thanks!
-Shawn
http://www.spidean.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: how do I use php://memory?

am 30.01.2010 02:17:41 von Eric Lee

--0016e68ed77bc982f6047e578434
Content-Type: text/plain; charset=UTF-8

On Sat, Jan 30, 2010 at 9:00 AM, Shawn McKenzie wrote:

> Mari Masuda wrote:
>
> > Has anyone ever successfully used php://memory before? If so, what
> > can I do to use it in my code? Thank you.
>
> No, but I was intrigued to try it, so I tested this:
>
> $text = 'Some text.';
> file_put_contents('php://memory', $text);
> echo file_get_contents('php://memory');
>
> And it returned nothing. The docs suck on this and it apparently
> doesn't work. I see others use it with fopen(), but there is no mention
> of which file functions it works with and which it doesn't.
>
>

Shawn

I did a sample test from the manual with fopen like this,



$fp = fopen('php://memory', 'r+');

if ($fp)
{
fputs($fp, "line 1\n");
}

rewind($fp);
echo stream_get_contents($fp);

?>

console output

F:\wc\trunk>php -f m.php
line 1



Regards,
Eric,

--
> Thanks!
> -Shawn
> http://www.spidean.com
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--0016e68ed77bc982f6047e578434--

Re: Re: how do I use php://memory?

am 30.01.2010 04:46:47 von Shawn McKenzie

Eric Lee wrote:
> On Sat, Jan 30, 2010 at 9:00 AM, Shawn McKenzie wrote:
>
>> Mari Masuda wrote:
>>
>>> Has anyone ever successfully used php://memory before? If so, what
>>> can I do to use it in my code? Thank you.
>> No, but I was intrigued to try it, so I tested this:
>>
>> $text = 'Some text.';
>> file_put_contents('php://memory', $text);
>> echo file_get_contents('php://memory');
>>
>> And it returned nothing. The docs suck on this and it apparently
>> doesn't work. I see others use it with fopen(), but there is no mention
>> of which file functions it works with and which it doesn't.
>>
>>
>
> Shawn
>
> I did a sample test from the manual with fopen like this,
>
>
> >
> $fp = fopen('php://memory', 'r+');
>
> if ($fp)
> {
> fputs($fp, "line 1\n");
> }
>
> rewind($fp);
> echo stream_get_contents($fp);
>
> ?>
>
> console output
>
> F:\wc\trunk>php -f m.php
> line 1
>
>
>
> Regards,
> Eric,

So maybe it only works with an open file/stream resource? Hard to tell
with no docs.


--
Thanks!
-Shawn
http://www.spidean.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: how do I use php://memory?

am 30.01.2010 05:07:52 von daniel.brown

--0016e6d58f7f62088c047e59e5e1
Content-Type: text/plain; charset=ISO-8859-1

(Typing from the DROID, so forgive the top-posting.)

Shawn, would you take a few moments to submit this as a bug at
http://bugs.php.net/? I know you well enough that, if you say the docs suck,
they probably do.

On Jan 29, 2010 10:47 PM, "Shawn McKenzie" wrote:

Eric Lee wrote:
> On Sat, Jan 30, 2010 at 9:00 AM, Shawn McKenzie >wrote:
>
>>...
So maybe it only works with an open file/stream resource? Hard to tell
with no docs.


--

Thanks!
-Shawn
http://www.spidean.com

--
PHP General Mailing List (http://www.php.net/)
To unsubsc...

--0016e6d58f7f62088c047e59e5e1--

Re: how do I use php://memory?

am 30.01.2010 07:57:40 von Mari Masuda

On Jan 29, 2010, at 4:38 PM, Nathan Nobbe wrote:

> On Fri, Jan 29, 2010 at 5:35 PM, Mari Masuda =
wrote:
> Hello,
>=20
> I have a function that uses tidy to attempt to clean up a bunch of =
crappy HTML that I inherited. In order to use tidy, I write the crappy =
HTML to a temporary file on disk, run tidy, and extract and return the =
clean(er) HTML. The program itself works fine but with all of the disk =
access, it runs quite slowly.
>=20
> why read from disk in the first place?
>=20
> http://us3.php.net/manual/en/tidy.parsestring.php
>=20
> -nathan=20

Thank you, this looks like exactly what I need. Unfortunately I cannot =
get it to work on my machine. I recompiled PHP with =
--with-tidy=3D/usr/local and this is the version and modules in use:

[Fri Jan 29 22:50:41] ~: php -vPHP 5.2.12 (cli) (built: Jan 29 2010 =
22:35:24) Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
[Fri Jan 29 22:52:30] ~: php -m
[PHP Modules]
ctype
date
dom
filter
gd
hash
iconv
json
libxml
mbstring
mysql
mysqli
pcre
PDO
pdo_mysql
pdo_sqlite
posix
Reflection
session
SimpleXML
SPL
SQLite
standard
tidy
tokenizer
xml
xmlreader
xmlwriter
zlib

[Zend Modules]

[Fri Jan 29 22:52:34] ~:=20


When I run this test code
==================== =3D
$html =3D =
"blah

hello

";
$config =3D array('indent' =3D> true,
'wrap' =3D> '0');

// Tidy
$tidy =3D new tidy();
var_dump($tidy);
$tidy->parseString($html, $config, 'utf8');
var_dump($tidy);
$tidy->cleanRepair();
var_dump($tidy);
echo tidy_get_output($tidy);
var_dump($tidy);
?>
==================== =3D

I get this output:
==================== =3D
object(tidy)#1 (2) {
["errorBuffer"]=3D>
NULL
["value"]=3D>
NULL
}
object(tidy)#1 (2) {
["errorBuffer"]=3D>
NULL
["value"]=3D>
NULL
}
object(tidy)#1 (2) {
["errorBuffer"]=3D>
NULL
["value"]=3D>
NULL
}
object(tidy)#1 (2) {
["errorBuffer"]=3D>
NULL
["value"]=3D>
NULL
}
====================

I have no clue what I'm doing wrong...

Mari=

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: how do I use php://memory?

am 30.01.2010 08:32:07 von Mari Masuda

On Jan 29, 2010, at 10:57 PM, Mari Masuda wrote:

>=20
> On Jan 29, 2010, at 4:38 PM, Nathan Nobbe wrote:
>=20
>> On Fri, Jan 29, 2010 at 5:35 PM, Mari Masuda =
wrote:
>> Hello,
>>=20
>> I have a function that uses tidy to attempt to clean up a bunch of =
crappy HTML that I inherited. In order to use tidy, I write the crappy =
HTML to a temporary file on disk, run tidy, and extract and return the =
clean(er) HTML. The program itself works fine but with all of the disk =
access, it runs quite slowly.
>>=20
>> why read from disk in the first place?
>>=20
>> http://us3.php.net/manual/en/tidy.parsestring.php
>>=20
>> -nathan=20
>=20
> Thank you, this looks like exactly what I need. Unfortunately I =
cannot get it to work on my machine.

[snip]

So I figured it out... I was using the wrong command to compile libtidy. =
(I am not a *nix geek so I had no idea I was messing it up.) To get it =
working, I followed the instructions I found here: =
http://www.php.net/manual/en/ref.tidy.php#64281

My setup is slightly different from the person who wrote the directions =
in that I am running OS X 10.6.2 and PHP 5.2.12. However, the only =
difference between the instructions I followed and what I actually had =
to do is that the line to comment out for me was line 525 instead of =
508. (The actual line is: typedef unsigned long ulong;)

Thank you everyone for your help!=

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: how do I use php://memory?

am 30.01.2010 18:18:43 von Shawn McKenzie

Daniel P. Brown wrote:
> (Typing from the DROID, so forgive the top-posting.)
>
> Shawn, would you take a few moments to submit this as a bug at
> http://bugs.php.net/? I know you well enough that, if you say the docs suck,
> they probably do.
>
> On Jan 29, 2010 10:47 PM, "Shawn McKenzie" wrote:
>
> Eric Lee wrote:
>> On Sat, Jan 30, 2010 at 9:00 AM, Shawn McKenzie >> wrote:
>>
>>> ...
> So maybe it only works with an open file/stream resource? Hard to tell
> with no docs.
>
>
> --
>
> Thanks!
> -Shawn
> http://www.spidean.com
>

Done. Thanks Dan. http://bugs.php.net/bug.php?id=50886

--
Thanks!
-Shawn
http://www.spidean.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: how do I use php://memory?

am 30.01.2010 18:30:45 von Daniel Brown

On Sat, Jan 30, 2010 at 12:18, Shawn McKenzie wrote:
>
> Done. =A0Thanks Dan. =A0http://bugs.php.net/bug.php?id=3D50886

Thank you, sir. I thanked you on Facebook when I saw the report
come in, but wanted to thank you properly here as well.

--=20

daniel.brown@parasane.net || danbrown@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
Looking for hosting or dedicated servers? Ask me how we can fit your budge=
t!

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php