cannot get html special chars from url - php

I'm very new to php and can't find answer to my issue.
I try to echo a string with special characters (ie. à,é) from the URL.
URL string:
preview.php?content=<p>blà é bla</p>
Expected echoed result: blà é bla
So I do this:
$cont = $_GET['content'];
echo $cont;
Result:
bl
So, even if my page has a <meta charset="UTF-8">, I tried:
$cont = $_GET['content'];
$cont = html_entity_decode($cont, ENT_COMPAT, 'UTF-8');
echo $cont;
Same result:
bl
I tried with a header (header('Content-Type: text/html; charset=UTF-8');) at top of the page with same result.
What's weird is that is I try this:
echo html_entity_decode("<p>blà é bla</p>");
or even this:
echo "<p>blà é bla</p>";
I get the expected result:
blà é bla
So, I don't think it's a charset issue but can't understand why it works with the literal string but not with the get variable, can someone help me?

Encode your URL with: urlencode and htmlentities for example:
$url = 'content=' . urlencode('content=<p>blà é bla</p>');
echo '<a href="preview.php?' . htmlentities($url) . '">';

#JustOnUnderMillions, is correct.
E.g:
If you try to access data from URL like
http://localhost/index.php?content=%3Cp%3Eblà%20é%20bla%3C/p%3E
echo $_GET['content'];
OUTPUT: bl
But when you pass data by encoding data like
http://localhost/index.php?content=%3Cp%3Ebl%26agrave%3B%20%26eacute%3B%20bla%3C%2Fp%3E
echo $_GET['content'];
OUTPUT: blà é bla
It is better to use urlecode function for encode data in URL.

Thanks all.
This works perfectly !
Actualy, as my URL is generated with javascript, I did a encodeURIComponent on my link's href variable and it works as well.

If you want to pass a URL with parameters as a value in a URL and through a javascript function, such as.
Pass the URL value through the PHP urlencode() function twice, like this
<?php
$url = "index.php?id=4&pg=2";
$url = urlencode(urlencode($url));
echo "<a href=\"javascript:openWin('page.php?url=$url');\">";
?>
On the page being opened by the javascript function (page.php), you only need to urldecode() once, because when javascript 'touches' the URL that passes through it, it decodes the URL once itself. So, just decode it once more in your PHP script to fully undo the double-encoding.
<?php
$url = urldecode($_GET['url']);
?>
If you don't do this, you'll find that the result URL value in the target script is missing all the var=values following ? question mark.
index.php?id=4
kindly check this online tool also : URL Encode , URL Decode

Related

cannot print url data in a required format via php

I need to get url data printed in the following format below
https://mysite/data/api?q="'xxxxx' in contents"
but each time i run the code below am getting something like this below which is not what I want
https://mysite/data/api?q=xxxxx in contents
here is what I have tried
$my_id = 'xxxxx';
$myurl= "https://mysite/data/api?q={$my_id}";
echo $myurl . " in contents";
If you want to print the quotes to the query parameter, you need to
Include your quote to the string, and
Encode the query in proper url encoding.
<?php
$query = http_build_query([
'q' => "\"'xxxxxxx' in contents\"",
]);
$myurl= "https://mysite/data/api?{$query}";
echo $myurl;
Which would output this:
https://mysite/data/api?q=%22%27xxxxxxx%27+in+contents%22
And is effectively this when copied and paste to your browser's location bar:
https://mysite/data/api?q="'xxxxxxx' in contents"
Try like this HTML code in your variable declaration:
<?php
$my_id = '"'xxxxx'';
$myurl= "https://mysite/data/api?q={$my_id}";
echo $myurl . " in contents" ";
?>
You will get the output like this:
https://mysite/data/api?q="'xxxxx' in contents"

Cannot pass variable with apostrophe in "a href" link

I select a list of names from mysqli database then display row details in display.php with if (isset($_GET['name']));
The link is
$str = strtoupper($str);
echo "<tr><td><a href='php/display.php?name=$str'>$str</a></td></tr>";
This executes correctly unless name contains '(apostrophe).
For instance $str (as input/click) shows as L'ECLIPSE but the <a> link only L'
The result in display.php is 'No data found for your request'
I have found exact same queries on this site but none of the answers have resolved my problem. Perhaps I am not implementing correctly.
I assume this is about escaping. But I know little about it.
<?php
$str = strtoupper($str);
echo "<tr><td><a href='php/display.php?name=".urlencode($str)."'>$str</a></td></tr>";
urlencode() the string first. So you don't get this kind of problems.
Try this code.
<?php
$str = strtoupper($str);
echo "<tr><td><a href='php/display.php?
name=".htmlspecialchars($str)."'>$str</a></td></tr>";
?>
Your Single quote becomes &#039 ;
I hope it will help

PHP, how to set a string variable as "</a>"? PHP thinks it is command.

I am just trying to write a simple page, where I create a clickable link for HTML.
I get the first part okay, but this part yields a blank string:
$URLString = "<";
$URLString .= "/a";
$URLString .= chr(62); // that is, ">"
echo "URLString = ";
echo $URLString; // shows blank space
Any idea how to get PHP to accept this as a string, and not a command?
Thanks for help!
If I understood you well, and I think I have, you want to use:
$URLString = "&lt"; //that is "<"
$URLString .= "/a";
$URLString .= "&gt"; // that is, ">"
echo "URLString = ";
echo $URLString; // shows blank space
This are characters that represend < and > for html. Which is what htmlentities internaly does and you can find that in PHP docs
<?php
$str = "A 'quote' is <b>bold</b>";
// Outputs: A 'quote' is <b>bold</b>
echo htmlentities($str);
?>
Here is a link http://php.net/manual/en/function.htmlentities.php
You should use htmlentities():
$URLString = "<";
$URLString .= "/a";
$URLString .= chr(62); // that is, ">"
echo "URLString = ";
echo htmlentities($URLString);
Replace < by < and > by >
It sounds to me like you would like to display a link on your page if I were you I would close out of the php and just write the html. For example
<?php
//Some actual php code would go here
?>
Look where this takes you!
If you just close out of the php the php parser will just output whatever text is there. You can even generate some dynamic stuff in your php code and easily output it using the <?= ?> tag in php. Like this:
<?php
$tagText = 'Look where this takes you!';
$tagHref = 'www.google.com';
?>
<?= $tagText ?>
Both of these blocks of code produce the same output.
Also like everyone else is saying in the comments, you wont be able to see just an <a> being sent to the browser. The browser doesn't display tags, it usually displays what is between an open and closing tag.
Check out http://www.w3schools.com/ for more info on all of this stuff and some great tutorials.

php, how to convert special characters to text?

xxxi have the_title() that returns some text, in this case Blue & Whiny
the problem as we can see is that the & character looks different
how do i turn Blue & Whiny into Blue & Whiny i tryed: htmlspecialchars_decode(the_title()), html_entity_decode(the_title()),htmlspecialchars(the_title()) and nothing.
i want to convert & to &
there is not much code to share, I just do this: <?php the_title() ?> and i get Blue & Whiny. If i use get_the_title() it wont display anything
Any ideas?
Thanks
edit1. ill share some code:
<script type="text/javascript">
function showShareUI() {
var act = new gigya.services.socialize.UserAction();
act.setUserMessage("Check out this article.");
act.setTitle("Trends on Explore Talent - <?php the_title(); ?>");
act.setDescription("<?php get_the_content(); ?>");
act.setLinkBack("<?php the_permalink(); ?>");
act.addActionLink("Check out this article", "<?php the_permalink(); ?>");
var image = {
src: 'http://xxx.com/wp-content/uploads/2011/05/BOTTOM_BANNER.jpg',
href: '<?php the_permalink();?>',
type: 'image'
}
act.addMediaItem(image);
var params =
{
userAction: act, // The UserAction object enfolding the newsfeed data.
onError: onError, // onError method will be summoned if an error occurs.
onSendDone: onSendDone // onError method will be summoned after
,showEmailButton: true
// Gigya finishes the publishing process.
};
gigya.services.socialize.showShareUI(conf, params);
}
function onError(event) {
alert('An error has occured' + ': ' + event.errorCode + '; ' + event.errorMessage);
}
function onSendDone(event)
{
document.getElementById('status').style.color = "green";
document.getElementById('status').innerHTML = 'The newsfeed has been posted to: ' + event.providers;
}
</script>
I've tried everything. This starts to annoy me...
html_entity_decode() is the correct way to do it.
html_entity_decode("Blue & Whiny");
Will produce:
Blue & Whiny
If it's not working, make sure you don't have another issue - such as passing a string to it that is double encoded, or running htmlentities() on the string again later.
Demo: http://codepad.org/BHXGWXJi
Double check with a literal string and var_dump() the output, you should see the decoded version. Then var_dump(the_title()), to make sure you are actually passing what you think you are to html_entity_decode().
html_entity_decode should do the trick. If not, try to specify the third parameter $charset.
Something like:
echo html_entity_decode(the_title(), ENT_QUOTES, 'UTF-8');
the_title() directly prints the title, so adding html_entity_decode() directly around that won't work. You can, however, stop it from printing with its third function argument. E.g.
<?php echo html_entity_decode(the_title('', '', false)) ?>
There's also get_the_title(), which doesn't directly print the title, but it requires the ID of the post you want the title of, in contrast with the_title, which prints the title of the current post in The Loop. So you need to do something like this:
<?php echo html_entity_decode(get_the_title($post->ID)) ?>
And actually, you should be able to simply do:
<?php echo $post->post_title ?>
The only reason these utility functions are there is to escape things for you and add tags and stuff. If you just want the raw input, you can print it directly.
This won't fix all of your issues, though, because you're echoing it inside a JavaScript string, so you need to escape certain characters. json_encode() should do the trick, but see the question "Pass a PHP string to a Javascript variable (including escaping newlines)" for more details.
Try this:
echo(mb_convert_encoding(the_title(), "UTF-8", "HTML-ENTITIES"));
see if this works for ya
$convmap = array (0x0, 0xffff, 0, 0xffff);
//$str = mb_decode_numericentity (the_title(), $convmap, 'UTF-8' );
$str = mb_decode_numericentity ("&", $convmap, 'UTF-8' );
echo $str;
http://www.php.net/manual/en/function.mb-decode-numericentity.php

$_SERVER['REQUEST_URI'] generating extra & in URL

Having an issue here where the
$_SERVER['REQUEST_URI']
is spitting out:
/dev/nava2/store/index.php?route=product/product&product_id=48
the actual URL is /dev/nava2/store/index.php?route=product/product&product_id=48
obviously, the difference being the & on the top vs. the & on the bottom
full code looks like this:
$currentpage = $_SERVER['REQUEST_URI'];
$classic = "/dev/nava2/store/index.php?route=product/product&product_id=48";
if ($currentpage == $classic)
{ $classicclass = "current";
}
else { echo "";}
Any suggestions?
& is the html entity corresponding to &. You can obtain to original string back with html_entity_decode :
$original = html_entity_decode($_SERVER['REQUEST_URI']);
You can use html_entity_decode() to get the actual url but the top one should work. I dont think you need to change anything. You could also use str_replace or preg_replace if you really need to change some parts of your uri.
echo html_entity_decode($_SERVER['REQUEST_URI']);

Categories