Php QR Code library not working? - php

I got it to display with text but when i pass it into
function getQRcode($qr){
include 'application/views/inc/qrcode.php';
$this->view->qrcode = QRcode::png($qr);
$this->view->render('user/qrcode');
}
It works fine but when this symbol '%' shows up in $qr variable it just wont work why is this ??
php qr code library: http://phpqrcode.sourceforge.net/

The code you're showing isn't particularly relevant as it doesn't show how you process the variable before you use it, nor the significant part(s) of the library.
At a guess you're passing in the value using GET/POST and aren't properly url-encoding the string. % is used for special sequences eg %20 represents a space.
A list of characters that need to be encoded is available here.
Try replacing your % with %25.
For future reference, an online encoder/decoder can be found at http://meyerweb.com/eric/tools/dencoder/

Related

&center causes ¢ sign to come in PHP

I want to save an url with parameters to a string.
One of the parameters is "center".
However, when I try to save it as www.xyz.com?r=1&center=34, it reads it as www.xyz.com?r=1¢er=34. I do not want to convert &cent to the cent sign. What is the proper procedure to do this?
Edit: Since this is receiving negative votes, I'd like to mention that in my case, I actually needed the raw string instead of escaping it. It is working now. Rendering on the HTML page was a problem, but the file_get_contents needed the exact url, and it is working.
If you still wish to downvote this question, please explain why.
Try this way,
$link = "www.xyz.com?r=1&center=34";
$result = str_replace("&", "&", $link);
echo $result;
It's because &something; is the HTML way of "escaping". Here are a few examples:
©: ©
×: ×
€: €
←: ← (left arrow)
→: → (right arrow)
&: & (the actual sign)
You can find more of those on w3schools. 😉
It's because you have to escape the & in URLs: &.
So, in your URL, you should replace that lonely & by &.It should look like this:
www.xyz.com?r=1&center=43

'&LTV' rendering as '<V' when I need it to literally say '&LTV' - php

I ran into a quirky syntax issue.
I am using php and cUrl to pull in data from a web page. The link has several variables. One of them is '&LTV', but the resulting link keeps translating '&LTV' as '<V', looking as '&LT' and the 'less than' symbol, when I need the literal text.
I have looked all over the place to figure out how to force php to read '&LTV' literally but have not found it.
Any ideas here would be appreciated.
Thanks.
You need to encode your HTML entities. Either use htmlentities or manually type out the string "&LTV".

Graph image unusable as PNG in script

I am trying to get some URL of graph from Google play. I parse its html page and get following URL: this. This link is stored in variable and when I try to display it by echo, it is shown correctly (It coincides with URL). But in case if I use this variable to create image by imagecreatefrompng, it doesn't work. Although, if I use constant value (The value of very variable which obtained by echo) - it works perfectly. So I decided to compare value of variable and constant value and they have difference! There are 245 differences between the two strings.
The first difference between the strings is at position 43 (It's & symbol). Why? Seems to me it replaces symbols such as & or %. How can I avoid this situation?
The problem was that & was replaced by &! Other symbols were identical. So, the following code solves this problem
str_replace("&", "&", $graphDownloads);
Hope it helps someone else.

Settings that could influence PHP str_replace behaviour

I am currently working on a replacement tool that will dynamically replace certain strings (including html) in a website using a smarty outputfilter.
For the replacement to take place, I am using PHP's str_ireplace method, which reads the code that is supposed to be replaced and the replacement code from a database, and then pass the result to the smarty output (using an output filter), in a similar way as the below.
$tpl_source = str_ireplace($replacements['sourceHTML'], $replacements['replacementHTML'], $tpl_source);
The problem is, that although it works great on my dev server, once uploaded to the live server replacements occasionally fail. The same replacements work just fine on my dev version though. After some examinations and googling there was not much I could find out regarding this issue. So my question is, what could influence str_replace's behavour?
Thanks
Edit with replacement example:
$htmlsource = file_get_contents('somefile.html');
$newstr = str_replace('Some text', 'sometext', $htmlsource); // the text to be replaced does exist in the html source
fails to replace. After some checking, it looks like the combination of "> creates a problem. But just the combination of it. If I try to change only (") it works, if I try to change only (>) it works.
It might be that special chars like umlauts do not display on the live server correctly and so str_replace() would fail, if there are specialchars inside the string you want to replace.
Is the input string identical on both systems? Have you verified this? Are you sure?
Things to check:
Are the HTML attributes in the same order?
Are the attribute values using the same kind quote marks? (eg <a href='#'> vs <a href="#">)
Is there any other stray HTML code getting in there?
Is the entity encoding the same? (eg vs   - same character; different HTML)
Is the character-set the same? (eg utf-8 vs ISO 8859-1: Accented characters will be encoded differently)
Any of these things will affect the result and produce the failures you're describing.
This was a trikcy problem, and it ended up having nothing to do with the str_replace method itself;
We are using smarty as a tamplating system. The str_replace method was used by a smarty ouput filter in order to change the html in some ocassions, just before it was delivered to the user.
Here is the Smarty outputfilter Code:
function smarty_outputfilter_replace($tpl_source, &$smarty)
{
$replacements = Content::getReplacementsForPage();
if (is_array($replacements))
{
foreach ($replacements as $replacementData)
{
$tpl_source = str_replace($replacementData['sourcecode'], $replacementData['replacementcode'], $tpl_source);
}
}
return ($tpl_source);
}
So this code failed now and then for now apparent reason... until I realized that the HTML code in the smarty template was being manipulated by an Apache filter.
This resulted into the source code in the browser (which we were using as the code to be replaced by something else) not being identical to the template code (which smarty was trying to modify). Result? str_replace failed :)

What are the precise rules/PHP function for encoding strings into POST arrays?

Just getting into PHP web development. I've got an HTML form where a user checks some series of dynamically-generated checkboxes, and submits via POST. On the PHP side, I want to check which of the check-boxes were clicked.
I have an array $full_list, and am doing something like
$selected_checkboxes = array_filter($full_list, function($item) {
array_key_exists($item, $_POST);
}
I run into problems when a list item is named, for example "Peanut Butter", since in the POST array it is named "Peanut_Butter".
I could certainly just str_replace " " with "_" before checking array_key_exists, but I imagine that there is a more fundamental encoding problem here; specifically, I'm not sure of exactly what layer transforms normal strings in HTML Forms (value="Peanut Butter") into "Peanut_Butter".
So:
what layer is responsible for this conversion? Is it the browser?
what are the exact conversion rules, and is there a PHP function out there that will replicate that exact conversion?
The accepted answer by Byron Whitlock is wrong. PHP is indeed the culprit here. See the PHP manual:
Dots and spaces in variable names are converted to underscores. For
example <input name="a.b" /> becomes $_REQUEST["a_b"].
Also refer to the answers of this similar question.
PHP doesn't do this. There is something on the client side that is converting spaces to underscores.
The browser should encode each variable using the equivalent of urlencode(). PHP will automatically decode these strings so it is transparent for the programmer.
edit
The equivalent in javaScript is escape(). But it is very very likely there is some js code manually converting spaces to underscores.

Categories