doule0941
2013-07-08 09:24
浏览 151
已采纳

在php脚本中返回shell_exec()函数的参数

I am calling executable file with main() function from php script using exec(). Which works fine but return all printf() values rather only returning array:

main.cpp:

#include<stdio.h>
#include<string.h>
#include "foo.h"
int main(int argc, char *argv[] )
{
    char buffer[1024];
    char *ch;
    static int ar[2];
    strcpy(buffer,argv[1]);
    printf("Client : 
");
    if ( argc != 2 ) /* argc should be 2 for correct execution */
    {
              printf( "
%s filename
", argv[0] );
    }
    else 
    {
        printf("
string is :%s 
",buffer);
    ch=foo(buffer);
    ar[0]=((int*)ch)[0];
    ar[1]=((int*)ch)[1];
    printf("Counts is :%d  %d 
",ar[0],ar[1]);
    }
    return (int)ar;

}

my test.php

<?php
$s="critic worst";
escapeshellarg($s);
$a=array(shell_exec('/home/technoworld/Videos/LinSocket/client "nice bad"'));
print_r($a[0]);
//echo exec('whoami');
?>

Which shows output:

Client : string is :nice bad Positive count: 2 Negative count: 2 array item2 2 Count is :2 2

I also tried with exec() which also gives same prob. Can any one suggest how to get ar[0] and ar[1] from main.cpp?

Client : string is :nice bad Positive count: 2 Negative count: 2 array item2 2 

This is from all printf() present in foo.cpp file.

When I use exec() then it gives Count is :2 2

How to get exact ar[0] and ar[1]

图片转代码服务由CSDN问答提供 功能建议

我使用exec()从php脚本调用带main()函数的可执行文件。 哪个工作正常,但返回所有printf()值,而只返回数组:

main.cpp:

  #include&lt; stdio.h&gt  ; 
 #include&lt; string.h&gt; 
 #include“foo.h”
int main(int argc,char * argv [])
 {
 char buffer [1024]; 
 char * ch; \  n static int ar [2]; 
 strcpy(buffer,argv [1]); 
 printf(“Client:
”); 
 if(argc!= 2)/ * argc应为2以便正确执行 * / 
 {
 printf(“
%s filename 
”,argv [0]); 
} 
 else 
 {
 printf(“
string is:%s 
”,buffer  ); 
 ch = foo(缓冲区); 
 ar [0] =((int *)ch)[0]; 
 ar [1] =((int *)ch)[1]; 
 printf  (“计数是:%d%d 
”,ar [0],ar [1]); 
} 
 return(int)ar; 
 
} 
    
 
 

my test.php

 &lt;?php 
 $ s =“crit worst”; 
escapeshellarg($ s); 
 $ a  = array(shell_exec('/ home / technoworld / Videos / LinSocket / client“nice bad”')); 
print_r($ a [0]); 
 // echo exec('whoami'); 
?&gt;  ; 
   
 
 

显示输出:

 客户端:字符串是: 好坏正计数:2负数:2数组项2 2计数是:2 2 
   
 
 

我也试过exec()也给出了同样的问题 。 任何人都可以建议如何从main.cpp获取ar [0]和ar [1]吗?

 客户端:字符串是:好坏正数:2 负数:2数组项2 2 
   
 
 

这是来自foo.cpp文件中的所有printf()。

我使用exec()然后它给出 Count is:2 2

如何获得精确的ar [0]和ar [1]

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duancenxie2233 2013-07-08 09:36
    已采纳

    Perhaps simply

    preg_match('/[0-9]*/', $output, $matches);
    

    If I'm not mistaken...

    To be sure:

    preg_match('Counts is :([0-9]*)[^0-9]*([0-9]*)/',$output,$matches);
    

    Also have a look at this question to find ouy how to use PHP and C(++) interactively

    A quick, and hacky ugly messy workaround could be:

    printf("%d",ar[0]);
    return ar[1];//end main function
    

    Then, in your PHP script:

    $a = array(shell_exec('/home/technoworld/Videos/LinSocket/client "nice bad"'),
               shel_exec('echo $?');
    );
    

    Then get the last numeric value from the output string, (preg_match('/([0-9]*)\w*$/',$a[0],$matches); should do it, and $a[1] will contain the value of ar[1] of the main.cpp file, because that was the exit code. Note that exit codes aren't for show. The signify something! Changeing them is generally a bad idea. I'd just change my main.cpp, and add the line:

    printf("@ar[0]=%d;ar[1]=%d@",ar[0],ar[1]);
    

    This prints a clearly formatted string, which is easy to parse using a regular expression. Assuming *ar held values 123 and 456 respectively:

    preg_match_all('/\]\=([^;@]*)/',$a[0],$matches);
    var_dump($matches[1]);//[123,456]
    

    Or better stil:

    $parts = explode('@', $a[0]);
    echo $parts[1];//ar[0]=123;ar[1]=456
    

    Bottom line:

    Add this to your main function in main.cpp:

    printf("@ar[0]=%d;ar[1]=%d@",ar[0],ar[1]);
    

    and, to get the int values in php:

    $a = array(shell_exec('..'));
    preg_match_all('/[^=]*\=([^;@]*)/',$a[0],$matches);
    var_dump($matches[1]);
    

    That should do it.

    $a = array();
    $b = array();
    if (preg_match_all('/[^=]*\=([^;@]*)/', shell_exec('your command'), $matches))
    {
       $a[] = (int) $matches[1][0];//optionally cast to int
       $b[] = (int) $matches[1][1];
    }
    

    That's all... not sure why $a and $b need to be arrays, but that's probably because you're going to call the client bin a couple of times.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douxuan3095 2013-07-08 15:02

    Thanks to Elias for help. This is the solution:

    <?php
    
    if (preg_match_all('/[^=]*\=([^;@]*)/', shell_exec('/home/technoworld/Videos/LinSocket/client "nice bad worst good"'), $matches))
    {
       $x = (int) $matches[1][0];//optionally cast to int
       $y = (int) $matches[1][1];
       echo $x. '<br/>'. $y. '<br/>'
    }
    
    ?>
    

    $x,$y contain ar[0] and ar[1] respectively

    评论
    解决 无用
    打赏 举报
  • douba6365 2013-07-14 06:11

    This will also work:

    if (preg_match_all('/[^=]*=([^;@]*)/', shell_exec("/home/technoworld/Videos/LinSocket/client '$s'"), $matches)) //Values stored in ma.
    {
        $x = (int) $matches[1][0];  //optionally cast to int
        $y = (int) $matches[1][1];
    }
    
    echo "<br/>"; echo "Positive count :$x";
    echo "<br/>"; echo "Negative count :$y"; echo "<br/>";
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题