Hubvill
Hubvill

Reputation: 504

PHP inside HTML attribute

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

Answers (3)

Jimmy Obonyo Abor
Jimmy Obonyo Abor

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

Y. Alvarez
Y. Alvarez

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

maxhb
maxhb

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

Related Questions