user983223
user983223

Reputation: 1152

pipe php function

I want to use php's strip tags in a bash script. I figured I could just cat the html file I want to use and use that input and pipe it into php and then pipe that into something else (sed). Is that possible? I'm not sure exactly how to pipe the output of file.html into the strip_tag function...maybe put it all in a variable? I want the following to keep just the anchor tags...in the following I put in dummy text for strip_tags string because I didn't know how to pipe file.html in:

cat file.html | php strip_tags("<p><a href='#'>hi</a></p>",'<a>') > removed_tags.html

Upvotes: 1

Views: 1606

Answers (2)

Ry-
Ry-

Reputation: 225124

You can read from STDIN in PHP using the stream URI php://stdin. As for executing it, you'll also need to quote the PHP code and use the -r option, as well as echoing the result. So here's the fixed script:

cat file.html | php -r "echo strip_tags(file_get_contents('php://stdin'), '<a>');" > removed_tags.html

Upvotes: 1

Explosion Pills
Explosion Pills

Reputation: 191789

Reading from stdin in PHP and writing a php script without a file are possible, but it's way more trouble than just writing a file like

<?php echo strip_tags(file_get_contents($argv[1]), '<a>');

...

$ php that-file.php file.html > removed_tags.html

Upvotes: 0

Related Questions