0

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">
1
  • What is the HTML output you are getting, and what are you trying to get? Commented Dec 18, 2015 at 17:45

3 Answers 3

1

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">
Sign up to request clarification or add additional context in comments.

1 Comment

This worked. Thanks you! Removing the double quotes seems so strange to me but it works.
1

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">

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.