0

How to insert html code inside an array in PHP?

I tried searching on internet but found nothing.

$html = array
('<IFRAME src="http://link1.com" width="1" height="1" 
scrolling="auto" frameborder="0"></IFRAME>' ,

'<IFRAME src="http://link2.com" width="1" height="1" 
scrolling="auto" frameborder="0"></IFRAME>' ,

'<IFRAME src="http://link3.com" width="1" height="1" 
scrolling="auto" frameborder="0"></IFRAME>');
print_r ($html);

When I tried to print_r, there was no result.

3
  • 5
    You sure there's no results? If you're doing the print_r onto a web page, the HTML will be interpreted as such ... right-click and view source to make sure ... Commented Mar 21, 2012 at 18:10
  • The way you built it is valid. It is working. And you say - 'there are no results'. What exactly do you get? Blank page, error, empty array... Commented Mar 21, 2012 at 18:11
  • @all Yeah, Iam sorry that I forgoten to see it on source code... :( anyways thank.. :D Commented Mar 21, 2012 at 20:43

3 Answers 3

2

There is no result because you are giving the output to the browser, which happily places three <iframe>s with invalid source URLs in your page. So you see nothing for the same reason you would see nothing if you did print '<p></p>';.

If you view the page source you will see your HTML is there.

Normally to see HTML markup as "plain text" you would need to pass it through htmlspecialchars -- however, that function works with strings and here you have an array. So if you want to print the contents as human-readable text, you need to do something fancier and use array_map:

print_r(array_map('htmlspecialchars', $html));
Sign up to request clarification or add additional context in comments.

Comments

2

If you are printing these and accessing the output through your browser, you won't see any string as they are parsed by your browser.

If you would like to have a look at the raw output, do

echo htmlspecialchars(print_r($html, 1), ENT_QUOTES, 'UTF-8');

2 Comments

Yes, this would do it but he would need to loop through the $html array though.
crap, I got htmlspec and print_r in wrong order... Thanks stef
0

I had no problem running the code you just posted. If you are outputting to a webpage the iframes are most likely there and you should just view your page source. Try this...

<?php
    $html = array('<IFRAME src="http://link1.com" width="1" height="1" scrolling="auto" frameborder="0"></IFRAME>',
    '<IFRAME src="http://link2.com" width="1" height="1" scrolling="auto" frameborder="0"></IFRAME>',
    '<IFRAME src="http://link3.com" width="1" height="1" scrolling="auto" frameborder="0"></IFRAME>');
    echo "<pre><code>";
    echo print_r ($html);
    echo "</code></pre>";
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.