echo plus sign php - php

I'm echoing a php string variable into html
<span class="title_resource">
<?php echo $titulo; ?>
</span>
But when the plus sign (+) is present, it turns into a space.
I've already tried using urlencode but then I get something like "Constru%25C3%25A7%25C3%"
Should I use something like string replace?
Thanks in advance
Update:
$titulo is setted using $_GET
if (isset($_GET['T']))// title
$titulo = $_GET['T'];
(...)
More clear, perhaps
I want to send exactly this text "Estudo de ax^2 + bx + c". The page receives by $_GET. When I print this value I get "Estudo de ax^2 bx c"

Its the way values as encoded to be sent over using GET, spaces are converted to + and + are converted to %2B.
If you really want to send the plus symbol over the form, then replace the spaces with plus
$text= str_replace(" ", "+", $text);
Next make sure your form uses correct enctype either application/x-www-form-urlencoded or multipart/form-data

I think you should be using urldecode not urlencode ...
<span class="title_resource">
<?php echo urldecode($titulo); ?>
</span>
If this does not work ... can you add the full script .. where you passing , are you using POST etc .. so that i can help you better
:)

Seems like your variable is going through urldecode

Related

How I have to encode url for Facebook sharing

I am using a JScript code like this:
shareLink = function (link) {
FB.ui({
method: 'share',
display: 'popup',
href: link,
}, function(response){});
}
and it is called in a line like this:
<?php
$sharedata = " ... parameters ... "
?>
<a href="javascript:shareLink('http://<?php echo $site; ?>/?v=<?php echo rawurlencode($sharelink); ?>');">
so, the destination script trasform parameter by using:
$str = rawurldecode($_GET["v"]);
the problem is when the url has a "+" character
using rawurlencode it its converted to "%2B"
zcu0xci%2FFMH2%2B7cLDPVP%2BgD7%2FwQJ%2FFT2Bw%3D%3D
but facebook change only "%2B" into "+" sign again:
zcu0xci%2FFMH2+7cLDPVP+gD7%2FwQJ%2FFT2Bw%3D%3D
and my script does not recognize it
EDIT:
if I echo the "v" parameter I get
zcu0xci/FMH2 7cLDPVP gD7/wQJ/FT2Bw==
instead of
zcu0xci/FMH2+7cLDPVP+gD7/wQJ/FT2Bw==
SOLUTION:
the solution I've found is to replace space by "+" before decode
$str = str_replace(" ","+",$str);
You don't need to use rawurldecode() on elements of the $_GET super-global variable. PHP decodes URL data for you.
In addition, you should generally use urlencode() to encode query string data. If you investigate the difference between these two functions, you'll see that urlencode() will translate any space character to the "+" character. This could possibly be the problem you're experiencing.
I'm having difficulty understanding the remainder of your question. You may need to work on your phrasing.

.php?title=test to echo "test";

I think the title says it all.
I have an URL (example: index.php?title=My title goes here) and I would like to insert the string in my page with echo.
http://mywebsite.com/index.php?title=Alphanumeric title with spaces (if I can't use spaces, I will need to replace any "_" with a space in the final result)
The title is <b><?php echo $title; ?></b>.
The title is Alphanumeric title with spaces.
So, how can I do it?
Thanks in advance.
try with $_GET['title'] you can use space it will decode by urldecode()
The title is <b><?php echo (!empty($_GET['title']) ? urldecode($_GET['title']):''); ?></b>.
$title = urlencode($title); to generate your URL Safe pagination and urldecode($title); for decoding it to a normal string again.
Use $_GET[] for this
$_GET[] is an array containing all data sent through the URL
So in your case, use $_GET['title].
Also remember to use the function isset() to check if the value is actually in the URL!!!

$_GET didn't not print out # symbol and text behind

i have a string send by $_GET['foo']="C# Programmer";
when I echo $_GET['foo'], it only print C
is any way to solve this problem when string container # or other symbols send by $_GET
Spaces and hashes (#) are not valid in HTTP URLs and will need to be encoded if you want to use it in parameter values. You can use urlencode() to create URL-safe paramter values.
The following should work:
foo=C%23%20Programmer
If you're trying to send the GET request from a different page, you'd want something like this:
<?php
$var = 'C# Programmer';
?>
Go
Now, in select.php, if you try to echo $_GET['foo'];, it'll display C# Programmer.
Make use of functions like urlencode for passing this kind of values, and urldecode to get this values. Try to make use of post method to avoid this kind of problems
You shoud use the function named urlencode which returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs.
<?php
$var = 'C# Programmer';
?> <!-- end of PHP code -->
<a href="select.php?foo=
<?php echo urlencode($var) ?> <!-- start and end of PHP code -->
"> Go </a>
Live example
For more information, read about urlencode.

Treating & as normal character with $_GET

I have an application that posts content to a MySQL DB via PHP. The PHP uses $_GET to pull the content from the URL and then inserts it into the DB.
This works great, but I have discovered an issue. If the user enters certain characters (", &, and others), the $_GET method does not properly separate the content from the URL.
Let's say the user posts this content:
I love blue & green
In this situation, the & symbol cuts the string after the word blue.
Is there any way for me to edit my PHP file to ignore the & symbol and to actually treat it as part of the variable it is supposed to $_GET? Any help would be great!
You can URLencode data before sending it to the PHP. It's a better solution.
Specials chars must not be used in a query string if those chars are in data.
In Javascript, you can use the escape function : escape(&ee) will give %26ee
The correct method is to urlencode the "&" caracter by the client : pass "%26" instead of "&"
you can use $_SERVER['QUERY_STRING']
from http://php.net/manual/en/reserved.variables.server.php
You could send the request as a base64 encoded string:
$string = base64_encode("This is my long string with &ampersands and 'quotes'");
print base64_decode($string);
Note that base64-encoded data takes about 33% more space than the original data.
From the manual:
http://php.net/manual/en/function.base64-encode.php
You also have urlencode
try to urlencode your string:
&
becomes
%26
it's a PHP function :
http://php.net/manual/fr/function.urlencode.php
What about, before creating Query string, encode it ?
$str = "I love blue & green ?=&˙Đ[]";
$str = urlencode($str);
echo $str;
Will return:
I%20love%20blue%20%26%20green%20%3F%3D%26%CB%99%C4%90%5B%5D
You have to URL encode the string before you pass it as a GET parameter. In this particular case you have to replace & symbol with %26.
This can be done for example using javascript right before you send the form.

remove %20 and pass value with & with $_GET

i am trying to send a value through link and get the value on other file using $_GET but the problem is the value has & in between two words and in url its coming like
list.php?v=Bakery%20&%20Cake%20Design
and when i echo this value in second page it come out to be bakery instead of bakery cake & design . Since i am sending this value from the first page via jquery on click i tried using encodeURI() to remove the %20 from the link but it still does't helps as the part after %20 & is not being printed on second page when i echo the value. my jquery code is
var vendor = $(this).text().replace(/\s/g,"%20"); in this i tried to remove it via replace too . But still no help .
You dont need to remove %20
just check that there should not be any space between the variable and the value!
This will produce %20 :
echo '<td><b><font color="#663300">Deactivate User</font></b></td>';
This will not:
`echo '<td><b><font color="#663300">Deactivate User</font></b></td>';`
Reason : just a whitespace between (php?id ='.$row)
On the JS side you need to use encodeURIComponent() on the item with the ampersand like.
var title = encodeURIComponent('bakery cake & design'); // bakery%20cake%20%26%20design
As opposed to encodeURI which:
Note that encodeURI by itself cannot form proper HTTP GET and POST requests, such as for XMLHTTPRequests, because "&", "+", and "=" are not encoded, which are treated as special characters in GET and POST requests. encodeURIComponent, however, does encode these characters. These behaviors are most likely not consistent across browsers.
Use the URL Decoder function
echo urldecode("A%20B") // will print 'A B'

Categories