Reputation: 504
I am looking for some help putting some PHP code inside an HTML attribute. I can't seem to make it work. I think I am escaping the quotes incorrectly or the php is not being concatenated with the html, but I can't seem to figure out. Can someone please offer some advice. Should I put the HTML inside php string and echo is out that way?
<?php $icon_hospital = htmlspecialchars('http://s15.postimg.org/6ct1w2o5z/hospitals.png'); ?>
<?php echo $icon_hospital; ?>
<img src="https://maps.googleapis.com/maps/api/staticmap?size=400x400&markers=icon:"<?php echo $icon_hospital ; ?>"%7CAlbany,+NY&sensor=false">
Upvotes: 0
Views: 830
Reputation: 7875
Use rawurlencode()
<?php $icon_hospital = rawurlencode('http://s15.postimg.org/6ct1w2o5z/hospitals.png'); ?>
<?php echo $icon_hospital ; ?>
<img src="https://maps.googleapis.com/maps/api/staticmap?size=400x400&markers=icon:<?php echo $icon_hospital ; ?>%7CAlbany,+NY&sensor=false">
http://php.net/manual/en/function.rawurlencode.php
Upvotes: 1
Reputation: 11
Try removing the double quotes from the php code in img src.
<?php $icon_hospital = htmlspecialchars('http://s15.postimg.org/6ct1w2o5z/hospitals.png'); ?>
<?php echo $icon_hospital; ?>
<img src="https://maps.googleapis.com/maps/api/staticmap?size=400x400&markers=icon:<?php echo $icon_hospital ; ?>%7CAlbany,+NY&sensor=false">
Upvotes: 1
Reputation: 8845
Use urlencode() instead of htmlspecialchars() and dont use quotes within the url:
<?php $icon_hospital = urlencode('http://s15.postimg.org/6ct1w2o5z/hospitals.png'); ?>
<?php echo $icon_hospital; ?>
<img src="https://maps.googleapis.com/maps/api/staticmap?size=400x400&markers=icon:<?php echo $icon_hospital ; ?>%7CAlbany,+NY&sensor=false">
Upvotes: 1