How to use unicode in CodeIgniter? - php

I am using CI framework for my project. If I use like this
$title= "प्रदेश"; // प्रदेश is written in nepali langauge.
echo $title; // It will display प्रदेश
Now I want to use
<a href = "<?php echo base_url("home/$title")?>"> //home is a controller
Now In home controller if I tried to display$title it will not show प्रदेश.
It will display like this %E0%A4%AA%E0%A5%8D%E0%A4%B0%E0%A4%A6%E0%A5%87%E0%A4%B6
Please help me.

The base_url calls urlencode on the string, which makes it into url entities.
A space becomes %20 and things like that.
The url that is displayed is actually correct and should be interpretted correctly by the server, even if it's gibberish according to humans.
If you really want the human readable characters for some reason you can do two approaches, urldecode the resulting url(not recommended):
echo urldecode(base_url("home/$title"));
or
echo base_url("home/").$title

Related

use a href inside html entities

I use html entities to secure my site.
And my client want to add link in his post using the CMS.
how to make exception in html entities?
my code example:
<p><?php echo h($row['message']) ?></p>
//h is my function for htmlentities
My code display this message:
"You can click this link Link"
//And I dont know my data insert '\'
//It become Link
If my question is not clear please ask.
Really appreciate.
I believe what you want to do is pass into the DB with htmlentities() so it doesn't mess with your DB. To retrieve them you would use html_entity_decode(). The html_entity_decode() converts all strings with HTML entities back to there original string.
http://php.net/manual/en/function.html-entity-decode.php
Hopefully this answers your question.
Edit:
Raw data retrieved: http://www.example.com
Through htmlentities it spits out the HTML entities, which the browser cannot interpret when attempting to find that page. The use of htmlentities() (please if I'm wrong correct me) is to encode user input before passing it anywhere else.
User input: <script>hacks</script>
Passed though htmlentities:
&ltscript&gthacks&lt (whatever backslash is)script&gt
(This way it can't mess with anything in your database, better example is the use of PHP/MySQL but I'm not well versed to give that exact example at the moment.)
However this would expose your site when decoding it as well and other precautions would have to be taken.
Try this :
<?php
$link = h(stripslashes($row['message']));
?>
You can click this link <a href='<?php echo $link; ?>'>Link</a>

Handling %22 in urls

I have a page that uses an input field to search, and then uses that same field to go across pages. It also accepts double quotes for exact searching.
The url needs to look like blahblah.com/search/%22querystuff%22, but it autodefaults to blahblah.com/search/"querystuff" which fails in the browser.
Is there any way to get it to stop doing that or do I need to look into a different method.
Try out the urlencode method.
Here is an example of how to use it:
<?php
$userinput = '"Hello world"';
echo '<a href="http://blahblah.com/search/', urlencode($userinput), '">';
?>

Encoding string in PHP

I currently working on PHP and at some part of code I generate URL string. I set the GET parameter to currencyCode. But before I add &. So, at result I must get &currencyCode but get ¤cyCode.
How do I fix it?
You need to create url this way using urlencode() function:
<?php
echo 'http://example.com/'.urlencode('&currencyCode');
I also think that your main problem is with displaying it. You should use:
echo htmlspecialchars('&currencyCode');
to display it.
Otherwise it seems browser change the first part of this string into: ¤ entity so you get something like that ¤cyCode and what makes you have display ¤ symbol what is ¤ and the rest of string cyCode
You should use urlencode() function in php to encode the url. Your code should look like this
<?php
echo 'http://yoursitename.com/'.urlencode('&currencyCode');

How do I pass one PHP url as a php variable in another URL without that syntax messing up the variables?

I have a download page that take arguments like the download URL, the download-counter data file url, and the page to return to after downloading.
It is arranged like so:
start.php?url=...&page=...&file=...
(Download url, redirect page, counter file)
The problem is, when the redirect page contains PHP arguments with ? and & symbols, the URL becomes a confusing mess for PHP to work with.
Example:
start.php?url=URLTEXT&page=page?test1=x&test2=xx&file=FILETEXT
What should happen:
url=URLTEXT
page=page?test1=x&test2=xx
file=FILETEXT
what happens:
url=URLTEXT
page=page?test1=x
test2=xx
file=FILETEXT
How could I substitute characters or somehow make these arguments pass correctly in php?
Thanks for any help you can give.
Well, I'm not sure how your "messed up" URL looks like. However the string after the "?" is called Query String, and you can decode/encode it with
urlencode($normalString); //will be encoded for use in URL
urldeocde($queryString); //will be decoded for "normal" use
EDIT:
Here is some short example:
echo "Encode for use in URL: ";
echo urlencode("this is a string & üäöllasdlk<bbb2");
echo "<br />";
echo "Decode to use it in your script: ";
echo urldecode($_SERVER['QUERY_STRING']);
Output:
Encode for use in URL:
this+is+a+string+%26+%C3%BC%C3%A4%C3%B6llasdlk%3Cbbb2
Decode to use it in your script: test=12
(Assuming you have a Querystring containing the variable test=12)
Just use htmlspecialchars function on your URL string:
http://php.net/manual/en/function.htmlspecialchars.php

Adding + between keywords PHP

In my tag cloud I have urls that look like this
../search.php?query=cat&select=all
../search.php?query=the cat&select=all
What I am trying to do is if the keyword is more than one word like so:
../search.php?query=the cat&select=all
Put a + between the words like this
../search.php?query=the+cat&select=all
My default search function uses the + in the searches performed by the form but I would also like my tag cloud to have the same thing. I am only doing this for looks. It works perfectly fine as is but I thought I would ask anyway. Can anyone help? this is what I am currently using for links in my tag cloud
echo " <a href='../search.php?query=$word&select=all' target='rel' title='".($word)."'>".($word)."</a>";
Thanks.
I think what you're looking for is the urlencode() function.
urlencode & urldecode is just the function you require.
echo " <a href='../search.php?query=".urlencode($word)."&select=all' target='rel' title='".($word)."'>".($word)."</a>";
and on the other page you can have urldecode
echo urldecode($_GET['query']);

Categories