decode htmlentities of a file in PHP - php

How can I convert/decode html entities of a file's contents (XML) in PHP.
I tried to run this on a command line:
perl -MHTML::Entities -ne 'print decode_entities($_)' /apps/www/mydir/xmlfiles/p34580600.xml >> /apps/www/mydir/xmlfiles/p34580600_1.xml
It works fine running it on command line but when I try to call it within PHP:
system("perl -MHTML::Entities -ne 'print decode_entities($_)' /apps/www/mydir/xmlfiles/p34580600.xml >> /apps/www/mydir/xmlfiles/p34580600_6.xml");
It creates the file but it is empty. I tried to use html_entity_decode but the XML file is just too big. 20megs at least.
Any help or suggestion is greatly appreciated.
Thanks,

try escaping the $ in the system call. It is possible php is looking for a variable $_ since you are using double quotes for the string command.

Related

ERROR_CACHE_MISS when passing value from PHP to bash

I am trying to pass some values from a PHP file to a BASH script. I am getting a ERROR CACHE_MISS response.
The variable 'coretown' holes the value 'Houston, TX'. It must be in that format for the bash script to work.
Results of a test to prove the variables are correct
WorkString531cdf6b8b3451.99781853 OutString531cdf6b8b3451.99781853 Houston, TX
Execute the bash script.
$errorTrap=shell_exec("./Find-Town.sh $workPath $outPath $coreTown");
Bash script:
#!/bin/bash
set -x
InFile="./zipcode.txt"
"$Work1"="$1"
"$OutFile"="$2"
"$InString"="$3"
echo "$1";
echo "$2";
echo "$3";
Returned by the 'echo' in the script:
WorkString531cdf6b8b3451.99781853 OutString531cdf6b8b3451.99781853 Houston,
Notice the state (TX) is missing. If I put 'echo "$4";' in there it will display the 'TX'.
Is one of these languages handling the content of 'coreTown' ('Houston, TX') as an array? If so, which one? Amd how do I fix it? My google searches did not address this problem.
Since $coreTown contains a space, that's being treated as an argument delimiter in the shell; you need to quote it or escape the space. Luckily, PHP has a function that does that for you: escapeshellarg.
$workPathEsc = escapeshellarg($workPath);
$outPathEsc = escapeshellarg($outPath);
$coreTownEsc = escapeshellarg($coreTown);
$errorTap = shell_exec("./Find-Town.sh $workPathEsc $outPathEsc $coreTownEsc");

Php exec : output without clean the file

I'm calling a php script using exec and I'm trying to make a simple log.
Currently I have this :
exec("php script.php $options > temp/log.txt");
If I execute once the result is wrote, but if I execute this multiple times it's always replaced by the last call.
Is there a way to just add the output at the end of the .txt, without replacing all the file ?
Thanks
This has nothing to do with php, you are looking for a shell feature:
exec("php script.php $options >> temp/log.txt");
Note the double >> in there. It appends the redirection instead of overwriting the target.

How to pass arguments in php exec()?

I am trying to run a .exe application with input file and argument.
With cmd I can successfully start the executable like this...
C:\Program Files\MyApp.exe "path\to\input file" argument
However, nothing happens when I simply copy paste the string above into the exec() function like this..
exec("C:\Program Files\MyApp.exe "path\to\input file" argument")
Do I need to escape parts of the string? How should I proceed?
Just pass the arguments like a normal calling from shell
ex:
exec("C:\Program Files\MyApp.exe \"path to\input file\" argument")
I had to use this format
php -q "./yii.php" migrate/up --interactive=0 --migrationPath=#vendor/pheme/yii2-settings/migrations

Using PHP to execute cmd commands

How do I properly execute commands in the command line using php? For example I'm using the command below in the command line to convert a docx file into a pdf file:
pdfcreator.exe /PF"D:\Documents\sample.docx
Now using PHP code I want to be able to execute the same command but nothing seems to be happening:
<?php
shell_exec('pdfcreator.exe /PF"D:\Documents\sample.docx"');
?>
Is this possible in PHP?If yes, how do I do it?
system("c:\\path\\to\\pdfcreator.exe /PF\"D:\\Documents\\sample.docx"");
try this.
Don't forget to escape your command with escapeshellcmd(). This will prevent you from having to use ugly backslashes and escape characters.
There are also other alternatives which may work:
`command` // back ticks drop you out of PHP mode into shell
exec('command', $output); // exec will allow you to capture the return of a command as reference
shell_exec('command'); // will return the output to a variable
system(); //as seen above.
Also, make sure your .exe is included within your $PATH variable. If not, include the full path for the command.

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
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
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

Categories