& is automatically turned into & !?

Permalink
Hey,

I have a little problem. On my pagetype I want to print a dynamic image, lets say in this form: <img src="anyurl/img.php?a=b&c=d">

But when I echo this, it automatically transfers the '&' into a '&' which will destroy the img-link and not display it (instead showing the alt-Text for the img).

How can I prevent that the & gets automatically replaced by & for this one line?

View Replies:
Lintu replied on at Permalink Reply
I mean & is automatically turned into & amp ; - the forums didn't display it right.

Any ideas?
JohntheFish replied on at Permalink Reply
JohntheFish
One way, other ways may be better depending on the overall situation:

// from php code
?>
<img src="<?php echo path;?>?a=<?php echo b;?>&c=<php echo d;?>">
<?php
// back in php code


Within the echo of b, d you may also need to escape.
Lintu replied on at Permalink Reply
Thank you for your reply.

That doesn't help, I tried both of this:
<?php (...) echo '&'; (...) ?>

<?php (...) ?> & <?php (...) ?>


In both cases it will print a & amp; and therefor destroy my dynamic links..

Any other idea? :(
JohntheFish replied on at Permalink Reply
JohntheFish
Where exactly are you putting the php?
Lintu replied on at Permalink Reply
It's a pagetype for a person page, placed in (...)/themes/[ThemeName]/Person.php
JohntheFish replied on at Permalink Reply
JohntheFish
The only other thing I can think of is that it could be the editor you are using to edit the file, converting the ampersand when it saves. Other than that, I have no more ideas.
Lintu replied on at Permalink Reply
I'm 100% sure that it's not about the editor.

Does concrete5 somehow convert all htmlentities? Any way to skip that for certain lines or files?
JohntheFish replied on at Permalink Reply
JohntheFish
What does the request for the image look like in the network tab of the developer console? Is it definitely the src that is corrupted, and not the url that comes in to ....img.php?
Lintu replied on at Permalink Reply
I checked this now. Even though the source code shows the URL with & amp; the webconsole has the right url request - at least it shows & instead of & amp; in the url it was loading.

However the result for the image is a HTTP 400 Bad Request. When I copy the URL from the webconsole to the address bar it can load the image though.

So I guess the request is done with the & amp; instead of & and it will only display it correctly in the console. (Because 400 Bad Request is exactl what I get if I put & amp; instead of & to the URL and browse it manually)


So the problem remains :(
arrestingdevelopment replied on at Permalink Reply
arrestingdevelopment
Lintu,

I am no PHP expert (trust me!), but a little Googling about URL encoding of HTML characters and I came across 2 different PHP functions that you may be able to use on your generated image source URL:

html_entity_decode: http://tinyurl.com/kthn8

htmlspecialchars_decode: http://tinyurl.com/yc7wqda

I don't know about the specifics of either one, but maybe if you wrap the src URL for your image in one or the other of these functions it will work correctly for you.

Hope that helps!

- John
Lintu replied on at Permalink Reply
I also googled this issue alot and found a few (including your 2) functions which may have helped. Unfortunatelly they don't, the result remains exactly the same if I use or don't use those functions.

The problem is probably that the & gets converted into a &amp ; after all the php file is executed. I don't know how concrete5 buffers the output, but I can say that I don't really like it. Either it's just missing a decent documentation or it's not optimized at all. I'm a little bit disappointed in concrete5 at this point.

Anyways thank you for trying to help me :)
Mainio replied on at Permalink Reply
Mainio
Just to make sure, in your examples above you have spaces " " on both sides of the ampersand "&", is that really how you're outputting it? Could we get the url for this site to see how it behaves?
Lintu replied on at Permalink Reply
This is how the link is shown:

(...).php?a=b&c=d


I just forgot to use the code block in the first post. Didn't know that the forums would eat the HTML entities.

Edit: omg even in the code block the forums ruin my format.. This:http://pastebin.com/zSphkiH5
Mainio replied on at Permalink Reply
Mainio
In your page source like that?

And how do you print out the link?

Please paste like 10 lines on both sides of the code to see it really in context and also the whole HTML source that the page request produces.