How to pass string variable to php function? - php

I am using exec() to execute the file. One of the argument I need to pass is string.
shell_exec('/home/technoworld/Videos/LinSocket/client "Hi hello"'); This works fine!
But when I take string into var i.e. $s="Hi hello" and
'shell_exec('/home/technoworld/Videos/LinSocket/client . "$s"')`. It does not works. If
'shell_exec('/home/technoworld/Videos/LinSocket/client . $s')' goes in infinite wait!
Any Idea how to pass $s to the function?

shell_exec('/home/technoworld/Videos/LinSocket/client "'.$s.'"');
or use double quotes to parse variables
shell_exec("/home/technoworld/Videos/LinSocket/client '$s'");
or
shell_exec("/home/technoworld/Videos/LinSocket/client \"$s\"");

Your best bet is to use the escapeshellarg command
shell_exec('/home/technoworld/Videos/LinSocket/client '.escapeshellarg($s));

Related

PHP add string to first and last

How to add " string into the first and last in PHP,
something like this :
hello world
into like this :
"hello" "world"
is there before-string and after-string function in PHP?
In PHP there are two basic ways to get output: echo and print.
In this tutorial we use echo (and print) in almost every example. So, this chapter contains a little more info about those two output statements.
For more datailes use http://www.w3schools.com/php/php_echo_print.asp
Simply use it like as
echo '"Hello" "World"';
You can simply use preg_replace like as
echo preg_replace('/(\w+)/',"\"$1\"","hello world");
Demo
try this:
<?php
$str="hello world";//your string
$str=str_replace(" ",'" "',$str); //replace all spaces with " "
echo '"'.$str.'"';//while displaying add double quotes in begning and end.
?>
There is no such function.
However you can try using split function on space of the string, then use implode() function with your glue or you can simply try using regex.Latter would give you a 1 line call to do this.

how can we inject a PHP variable into Javascript

I have a problem with the "windows.location" command in JavaScript. I would like to add the php variable in the link windows.location. How can i do?
For example: I would like to transfer user to English page or Vietnamese Page by variable $lang
Here is my code
echo 'window.location="/B2C/$lang/confirm_fone.html"';
and the result in address bar is:
http://10.160.64.4:1234/B2C/$lang/confirm_fone.html
the $lang in address bar cannot be decode?
Variables in single-quoted strings don't get interpolated in PHP.
Use this instead:
echo 'window.location="/B2C/' . $lang . '/confirm_fone.html"';
Or use doublequotes:
echo "window.location='/B2C/$lang/confirm_fone.html'";
This is because the whole string is in single quotes.
You'll want to use double quotes for interpolation.
Otherwise, you can try:
echo 'window.location="/B2C/'.$lang.'/confirm_fone.html"';
If you put php variables within the string you should use Double Quotes .
echo "window.location='/B2C/$lang/confirm_fone.html'";
You have to concatenate the value, as follows :
echo 'window.location="/B2C/'.$lang.'/confirm_fone.html"';
Variables are not resolved by PHP in Strings when you use the ' as delimiter. Use " instead (and ' for the javascript command) or concatenate the String using ..
If you are in php-context:
echo "window.location=\"/B2C/"{$lang}"/confirm_fone.html\";';
If you are in HTML-Context (or better "outer-php-context"):
window.location="/B2C/<?php echo $lang ?>/confirm_fone.html";

PHP string contains $ and incorrectly recognizes variable

Basically I'm sending a formatted string to a PHP script via POST
So say I have the string... "abcdef$ghikl" it will recognize $ghikl as a variable...
Is there any ways to tell PHP that no variables exist within this string?
I know how to hard code it, you just use "'", but since the string is being sent to the script, I don't know what to do...
Thanks guys
For the scope of the question, all the code that's really needed is this:
$string = $_POST['string'];
// a couple strcmp's here, just to see if the string == ""
$array = explode("+", $string);
Try escaping the $
"abcdef\$ghikl"
PHP won't see that as a variable unless you eval() it. Can you show us your code?

PHP URL in query string using GET

I have a URL like:
http://www.google.com/test.html?d=1232&u=32
and I want to add it as a part of a GET query string like:
http://www.mysite.com/index.html?a=123&d=http://www.google.com/test.html?d=1232&u=32
Note the double "d" used. I want the URL sent to be just a url and not be read for it's query string...
What is the best way to do this to avoid problems?
You can use the urlencode() function.
Example:
$url = 'http://www.mysite.com/index.html?a=123&d='
. urlencode('http://www.google.com/test.html?d=1232&u=32');
You can use urlencode() to put that in the URL without having it interfere with anything else you have in there.
URL-encode the second url:
http://mysite.com/index.html?a=123&d=<?php echo urlencode('http://google.com/etc..'); ?>
You can assign a url to a variable and have it be query-string safe by using urlencode() (http://us3.php.net/urlencode). So you could do:
$url = 'http://www.mysite.com/index.html?a=123&d=' . urlencode('http://www.google.com/test.html?d=1232&u=32');
In this example the query-string var 'd' now houses all the contents of the second url. You will have to urldecode() it on the receiving end in order to extrapolate the data.

php combining url with variable

I want to do define the following variable $url
$url = www.example.com/$link;
where $link is another predefined variable text string e.g. testpage.php
But the above doesn't work, how do I correct the syntax?
Thanks
Try this:
$url = "www.example.com/$link";
When string is in double quotes you can put variables inside it. Variable value will be inserted into string.
You can also use concatenation to join 2 strings:
$url = "www.example.com/" . $link;
Hate to duplicate an answer, but use single quotes to prevent the parser from having to look for variables in the double quotes. A few ms faster..
$url = 'www.example.com/' . $link;
EDIT: And yes.. where performance really mattered in an ajax backend I had written, replacing all my interpolation with concatenation gave me a 10ms boost in response time. Granted the script was 50k.
Needs double quotes:
$url = "www.example.com/$link";
Alternate way:
$url = "www.example.com/{$link}";
$url = "www.example.com/$link";
It'd be helpful if you included the erroneous output, but as far as I can tell, you forgot to add double quotes:
$url = "www.example.com/$link";
You will almost certainly want to prepend "http://" to that url, as well.

Categories