Dan A Dan A - 3 months ago 18
PHP Question

Dissapearing "+" in php string where "-" shows up fine

How can I fix or avoid a missing + symbol in my string.
I used the same code with the - and it works fine.
I have tried using $num1."+".$num2 with no luck, very strange.
Photo of broblem

if(stripos ($text, 'add') !== false){
$trimtext = substr($text,3);
$num1 = strstr($trimtext, '+', true);
$trimnum2 = strstr($trimtext, '+', false);
$num2 = str_replace("+","",$trimnum2);
$result = $num1 + $num2;
file_get_contents("$website/sendmessage?&parse_mode=HTML&chat_id=$chatid&text=<code>$num1 + $num2 = $result</code>");
}

if(stripos ($text, 'sub') !== false){
$trimtext = substr($text,3);
$num1 = strstr($trimtext, '-', true);
$trimnum2 = strstr($trimtext, '-', false);
$num2 = str_replace("-","",$trimnum2);
$result = $num1 - $num2;
file_get_contents("$website/sendmessage?&parse_mode=HTML&chat_id=$chatid&text=<code>$num1 - $num2 = $result</code>");
}

Answer

Your problem is that "+" is a space in URL encoding. You need to replace "+" with "%2B" in the query string.

To correct Nehal's answer, you want

file_get_contents("$website/sendmessage?&parse_mode=HTML&chat_id=$chatid&text=".urlencode("<code>$num1 + $num2 = $result</code>"));

You don't want to URL encode the contents of the $website variable or the other query parameters, but rather to prepend them to the URL encoded string.

Comments