How to display this HTML string in echo php function

19,877

Solution 1

like this:

<?php
echo '<a href="http://www.mysite.com/'.$username.'"><img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a>';
?>

Or, like this:

<a href="http://www.mysite.com/<?=$username?>"><img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a>

You may want to make sure $username is safe though... at least use urlencode, htmlspecialchars, or something similar.

*EDIT* I had assumed you already knew how to get $username from the form you mentioned, but in case you didn't, you would just do:

$username = $_GET['username'];

Or you could use this as an opportunity to use those functions I mentioned above (unless you will be needing $username for some other purpose before echoing it out.

Example:

$username = urlencode($_GET['username']);

Or you could do this straight in the echo like this:

<a href="http://www.mysite.com/<?=urlencode($_GET['username'])?>"><img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a>

Solution 2

echo "<a href=\"http://www.mysite.com/" . htmlspecialchars($username) . "\"><img src=\"http://www.mysite.com/images/logo.jpg\" width=\"50\" height=\"50\" alt=\"La mia pagina su Mysite\"/></a>";

Solution 3

You can enclose echo in double quotes and the html attributes in single quotes

Use the below code if u get the username from form.

$username= htmlspecialchars($_REQUEST['username']);

or Use the below code if u assign the variable.

$username= htmlspecialchars(your text goes here...);

echo "<a href= 'http://www.mysite.com/$username'><img src='http://www.mysite.com/images/logo.jpg' width='50' height='50' alt='La mia pagina su Mysite'></a>";

Solution 4

echo sprintf('<a href="http://www.mysite.com/%s"><img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a>', htmlspecialchars($username, ENT_QUOTES, 'UTF-8'));
Share:
19,877
Admin
Author by

Admin

Updated on July 26, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm a newbie in php... I have a form who pass a username variable to a php scrit, this is the code.. <form action="bottone.php" method="get"> Inserisci il tuo nome utente: <input name="username" type="text" /> <input type="submit" value="GENERA CODICE" /> </form>

    I would like to display this HTML code in the botton.php script:

    <a href=www.mysite.com/$username <img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a>
    

    where $username is the variable passed from the form... how can I do that with an echo function?? Thanks

  • Quentin Pradet
    Quentin Pradet about 12 years
    Option #2 is better. Simply add htmlspecialchars(). :)
  • mason81
    mason81 about 12 years
    I prefer option #2 myself. Would htmlspcialchars() be better than urlencode() in this case?
  • Basti
    Basti about 12 years
    You insert the $username into the href attribute, so it's a URL. (raw)urlencode is the way to go here! :-)
  • mason81
    mason81 about 12 years
    Oh, I didn't realize there was a rawurldecode. Interesting, thanks! php.net/manual/en/function.rawurlencode.php
  • mickmackusa
    mickmackusa about 2 years
    echo sprintf() is an "antipattern". There is absolutely no reason that anyone should ever write echo sprintf() in any code for any reason -- it should be printf() every time.