我想从我的php传递字符串
<?php
str1="string to pass"
#not sure about passthru
?>
我的tcl
脚本
set new [exec $str1]#str1 from php
puts $new
这可能吗?请让我知道我坚持这个
答案 0 :(得分:1)
有可能。
<强> test.php的强>
<?php
$str1="Stackoverflow!!!";
$cmd = "tclsh mycode.tcl $str1";
$output = shell_exec($cmd);
echo $output;
?>
<强> mycode.tcl 强>
set command_line_arg [lindex $argv 0]
puts $command_line_arg
答案 1 :(得分:1)
最简单的机制是将Tcl脚本作为运行接收脚本的子进程运行(您可能将其放在与PHP代码相同的目录中,或者放在其他位置),这会解码传递的参数哪个可以满足你的要求。
所以,在PHP方面你可能会这样做(请注意重要在这里使用escapeshellarg
!我建议使用带空格的字符串作为测试用例,以确定代码是否正确引用):
<?php
$str1 = "Stack Overflow!!!";
$cmd = "tclsh mycode.tcl " . escapeshellarg($str1);
$output = shell_exec($cmd);
echo $output;
echo $output;
?>
在Tcl方面,参数(在脚本名称之后)被放入全局argv
变量的列表中。该脚本可以通过任意数量的列表操作将它们拉出来。这是一种方式,lindex
:
set msg [lindex $argv 0]
# do something with the value from the argument
puts "Hello to '$msg' from a Tcl script running inside PHP."
另一种方法是使用lassign
:
lassign $argv msg
puts "Hello to '$msg' from a Tcl script running inside PHP."
但是请注意(如果您使用Tcl的exec
来调用子程序),Tcl会自动为您自动引用参数。 (事实上,由于技术原因,它确实在Windows上执行。)Tcl不需要escapeshellarg
之类的东西,因为它将参数作为字符串序列而不是单个字符串,因此更多地了解正在发生的事情。
传递值的其他选项是环境变量,管道,文件内容和套接字。 (或者通过更具异国情调的东西。)两种语言之间的进程间通信的一般主题可能变得非常复杂,并且涉及很多权衡;你需要非常确定你要做的事情,以便明智地选择一个选项。