send parameters from php to c++ - php

I want to send some string parameter to a cpp.exe from PHP thanks to exec function. The aim of the exe is to compute a rank of documents according to a query.
Here is the code in php :
$query = "is strong";
$file = "vsm.exe $query";
exec($file,$out);
echo $out[0];`
I received this output for echo $out[0];
Notice: Undefined offset: 0 in C:\xampp\htdocs\analysis.php on line 25
But, my vsm.exe only work (meaning I receive my ranks in the $out variable as a string which is okay) when the query is without space:
$query = "is";
$file = "vsm.exe $query";
exec($file,$out);
echo $out[0];
I followed that example which works with integer parameter (this is not what I want, I want to send sentences):
$a = 2;
$b = 5;
exec("tryphp.exe $a $b",$c_output);
echo $c_output[0];
$c_array0 = explode(" ",$c_output[0]);
echo "Output: " . ($c_array0[0] + 1);
echo "Output: " . ($c_output[0] + 1);
How could I send strings including spaces (could be long text) as parameters to c++?
Thanks in advance.

I actually find something but still not enough good:
$query = "is strong";
$file = 'vsm.exe "is strong"';
exec($file,$out);
echo $out[0];
It returns me the rank I wanted. However, I look for a way to use $query as a parameter, not directly the string "is strong".

Related

function to get the subtraction result state that is it positive or negative?

I am performing subtraction on two variable.
$first_variable = 20;
$second_variable = 30;
$result = $first_variable - $second_variable;
So how do i get that the result $result is positive or negative?
Have any PHP function to determine that the result of subtraction is positive or negative?
I know the i can use if statement to get it done. but i am asking for any predefined function to do it.
The reason i asked it hear is just a curiosity.
You can use php gmp_sign function to achieve that check this
Example:-
<?php
// positive
echo gmp_sign("500") . "\n";
// negative
echo gmp_sign("-500") . "\n";
// zero
echo gmp_sign("0") . "\n";
?>
output
1
-1
0
Well, for pure academic purposes, you can sort of do it with a function:
$sign = sprintf("%+d", $number)[0];

Can we read character by character in PHP without using any builtin function...?

I want to write the algorithm for reading character by character from string in PHP. Is it possible to do this without using any builtin function...? If not possible, then can we do this by using minimum function (i.e count/size)....?
A string in PHP is in fact an array and can be addressed as such
$str = 'Hello World';
echo $str[0]; // H
echo $str[1]; // e
yes for example the string is "Hello world" then here is the code to access it
<?php
$var = "Hello World!!" ;
for($count = 0 ; $count < strlen($var) ; $count ++)
{
echo $var[$count] ;
}
?>
The above code traverses the string linearly till the value of counter is less than the string length owing to the fact that the counter was initialised to 0, Hope it helps.

Use my php variable as a formula to calculate something

I'm working in an invoice system, that has to calculate different formulas for every product, which will need to be filled with Width, Height, and Material cost.
I did a database where i'm storing
t_title -> "Curtain 1"
t_formula -> "({ANCHO}*{ALTURA})*{PRECIO}"
and then php does this:
<?php
$ancho = str_replace("{ANCHO}", $_POST['ancho'], $articulo['t_formula']);
$alto = str_replace("{ALTURA}", $_POST['alto'], $ancho);
$precio = str_replace("{PRECIO}", $_POST['precio'], $alto);
$total = $precio; echo eval($total);
?>
and the result is not giving anything, but a blank space.
How can i make it to work? I know that php can't calculate from variables as php but, i can't find another way to do it.
The eval() function expects the string parameter to be a proper code statement.
$str = '(10*10) * 1000';
echo eval($str);
will give you an error, because php cannot evaluate that string. However,
$str = 'return (10*10) * 1000;';
echo eval($str);
will evaluate the expression correctly.
You should use:
$total = 'return ' . $precio . ';';
echo eval($total);
Your using eval is wrong. The string passed to eval must be valid PHP code. i.e:
$precio2 = '$total = '.$precio.';';
eval($precio2);
echo $total;
http://php.net/manual/en/function.eval.php

How can I echo characters before and after a string?

I'm familiar with some of PHPs string functions, however I can't seem to find the right one to do what I want. What I'm trying to accomplish is to echo 22 characters before and after a string that I find. I'm able to find the starting position of the string as well as the string length...I'm thinking that these might be useful to accomplish what I need to. For example, let's say I find the string "hello". I want to echo "This is an example of hello how are you doing????" Will substr() accomplish this?
$var2 = 'hello';
$startingpos = strpos($var1, $var2));
$strlength = strlen($var2);
UPDATE: I've found the solution to my problem: Please see the below:
$additional_length = 2;
$startingpos = strpos($var1, $var2));
$strlength = strlen($var2);
if (($startingpos - $additional_length) < 0)
$start = 0;
else
$start = $startingpos - $additional_length;
echo substr($var2, $start, ($strlength + (2* $additional_length)))
You can easily set a constant inside your string, and then replace it with str_replace function.
For instance:
$results = str_replace("[FOUND]", "hello", "This is an example of [FOUND] how are you doing");
Working example:
function.onl/str_replace("[FOUND]", "hello", "This is an example of [FOUND] how are you doing");
Demo

read single byte as an unsigned byte

I have some a java server that I'm trying to get to play with a php script.
The format provides the number of bytes the message will take as an unsigned byte, and then the bytes that comprise the string.
here's my function with commentary
function read($socket, $readnum) {
$endl = "<br>"; // It's running on apache
// attempt to get the byte for the size
$len = unpack("C",socket_read($socket, 1))[0];
// this prints "after ", but $len does not print. $endl does
echo "after " . $len . $endl;
// obviously this fails because $len isn't in a valid state
$msg = socket_read($socket, $len);
// print the message for debugging?
echo $len . " " . $msg;
return $msg;
}
I'm not really a php guy, more of a Java guy, so I'm not sure how I should go about getting this length.
On #c0le2's request, I made the following edit
$lenarr = unpack("C",socket_read($socket, 1));
$len = $lenarr[0]; // line 81
Which gave the following error
PHP Notice: Undefined offset: 0 in simple_connect.php on line 81
The unpack format string is actually like code [repeater] [name], separated by forward slashes. For example, Clength. The output array will be associative and keyed by name, with repeated fields having a numeric suffix appended, starting at 1. E.g. the output keys for C2length will be length1 and length2.
The documentation is not super-clear about this.
So when you don't specify any name, it just appends the numeric suffix, starting at 1. So the length you are looking for is $lenarr[1].
But you should try this instead:
$v = unpack('Clength', "\x04");
var_export($v); // array('length' => 4)
Here are some other examples:
unpack('C2length', "\x04\x03");
// array ( 'length1' => 4, 'length2' => 3, );
unpack('Clength/Cwidth', "\x04\x03");
// array ( 'length' => 4, 'width' => 3, );
Also, in php you can't generally use array-access notation on an expression--you need to use it on the variable directly. So functioncall()[0] won't work, you need $v = functioncall(); $v[0]. This is a php wart.
You can't access a returned array like that. You'd do $len[0] after calling unpack().
$len = unpack("C",socket_read($socket, 1));
echo "after " . $len[0] . $endl;

Categories