3

I'm facing a problem that probably has an easy solution, but as I'm not a php expert I can not find it. I usually do this when I have to call a shell command from php:

cmd = "convert file.pdf image.jpg";
shell_exec($cmd);

But now I have a command that works on the shell that I can not make it run from the php, so I think there is probably a way to enunciate the same command but in php language,

the command:

for i in $(seq --format=%3.f 0 $nf); do echo doing OCR on page $i; tesseract '$imgdir/$imgdir-$i.ppm' '$imgdir-$i' -l eng; done

my php try:

<?php
$imgdir = "1987_3";
$nf = count(new GlobIterator('filesup/'.$imgdir.'/*'));
$cmd = "for i in $(seq --format=%3.f 0 $nf); do echo doing OCR on page $i; tesseract '$imgdir/$imgdir-$i.ppm' '$imgdir-$i' -l eng; done"
shell_exec($cmd);
?>

What I get is:

PHP Notice:  Undefined variable: i in count.php on line 7

suggestions are very welcomed... thanks

UPDATE

I have read the question from the one I'm marked as possible duplicated, and what I understood from that was that my "i" have to have a reference, which, for a shell command, it has, but it does not work when executed from php.

In regard of that I also tried this unsuccessfuly:

<?php
$imgdir = "1987_3";
$nf = count(new GlobIterator('filesup/'.$imgdir.'/*'));
$cmd ="seq --format=%3.f 0 $nf";
$i = shell_exec($cmd);
$cmd = "tesseract 'filesup/$imgdir/$imgdir-$i.jpg' 'filesup/$imgdir/$imgdir-$i' -l eng; done";
shell_exec($cmd);
?>
1

1 Answer 1

3

PHP will evaluate all variables inside a string with double quotes, example:

<?php
    $i=5;
    echo "Your i is: $i";
?>

output: Your i is: 5

If you want to avoid this behaivor, use a simple quote:

<?php
    $i=5;
    echo 'Your i is: $i';
?>

output: Your i is: $i

Update your code like this:

<?php
    $imgdir = "1987_3";
    $nf = count(new GlobIterator('filesup/'.$imgdir.'/*'));
    $cmd = 'for i in $(seq --format=%3.f 0 $nf); do echo doing OCR on page $i; tesseract \'' . $imgdir/$imgdir . '-$i.ppm\' \'' . $imgdir . '-$i\' -l eng; done';    
    shell_exec($cmd);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, I had to do some minor adjustments to the code, but it did work, thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.