First picture is my code.
I get the Category id "CatID" and Category Title "CatTitle" from the previous page
The results however does not display correctly when there is/are a space/'s in the title. How do i fix this ?
Any help appreciated
Thank you
Sam
you can use urlencode($string) and urldecode($string) functions.
for your case
$catTitle = urldecode($_GET['catTitle']);
%20 shows whenever there is space in URL stirng
The "&" symbol is an example of special character in XML and HTML called entities and must be translated to "&"
Do use the htmlentities funtion to make the translation:
echo htmlentities($CatTitle);
Update: Also consider that the URL may be interpreted as if the $_GET["CatTitle"]="Home " and the $_GET[" Articles"] = null. May be it should be: CatTitle=Home%20&%20Garden in which case you may want to use URL encode while genereting the link.
Related
For some reason when preg_replace sees ¬ in string and replaces it with ¬:
$url= "http://something?blah=2&you=3&rate=22¬hing=1";
echo preg_replace("/&rate=[0-9]*/", "", $url) . "<br/>";
But the output is as follows:
http://something?blah=2&you=3¬hing=1 // Current result
http://something?blah=2&you=3¬hing=1 // Expected result
Any ideas why this is happening and how to prevent it?
& has special meaning when used URIs. Your URI contains ¬, which is a valid HTML entity on its own. It's being converted to ¬, hence causing the trouble. Escape them properly as ¬ to avoid this problem. If your data is fetched from elsewhere, you can use htmlspecialchars() to do this automatically.
Use this & in place of this &
because your &no has special meaning
use this url :
http://something?blah=2&you=3&rate=22¬hing=1
and then do your replace accordingly
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 &ersands 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.
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'
I am creating links in an XSL stylesheet but it will not accept '&' only '& a m p;' (I had to put spaces between the characters so that this CMS would not convert it to its equivalent &) so my links look like href="home.com?first=GET1& a m p;second=GET2" instead of href="home.com?first=GET1&second=GET2" but when php recieves the variable the '&' is split and my second variable becomes [amp;second]=>GET2. Can you please help me to address this problem?
You must use &name=value.
& is just the way an ampersand must be specified in any XML document -- it is still just an ampersand.
Example:
A Link
I am trying to send a variable from a Javascript to a php script but what gets sent is just the first string and the rest is discarded.Dont know what i would be doing wrong.
Here is my code:
<script type="text/javascript">
document.write(<li><a href=../../../../projects/sungrant/view/HistoricalCategory2.php?category=Historical Category 2>Historical Category 2 </a></li>)
</script>
My $_GET['category'] at the server side only prints Historical? Dont know what i may be missing or if there is a better way of passin data from a Javascript to PHP,i will appreciate.
Either wrap your href attribute value in quotes or change the spaces to %20.
href="../../../../projects/sungrant/view/HistoricalCategory2.php
?category=Historical Category 2"
or
href=../../../../projects/sungrant/view/HistoricalCategory2.php
?category=Historical%20Category%202
The reason it doesn't work with spaces is that in valid HTML, attributes are separated by spaces. If you need to use spaces in a HTML attribute value, make sure you wrap the string with quotes. If it's a URL, the browser will do the necessary URL encoding for you.
The problem is with your URL - you havent encoded the spaces so it only picks up the first variable
../../../../projects/sungrant/view/HistoricalCategory2.php?category=Historical Category 2
should be
../../../../projects/sungrant/view/HistoricalCategory2.php?category=Historical+Category+2
or
../../../../projects/sungrant/view/HistoricalCategory2.php?category=Historical%20Category%202