PHP Issue With in_array function
- From: james.homme@xxxxxxxxxxxx
- To: programmingblind@xxxxxxxxxxxxx
- Date: Tue, 26 Aug 2008 14:35:23 -0400
Hi,
I don't know how well this program is going to look coming across your
email. I am very sure I am sending in_array the right stuff. It's all upset
because it thinks the second parameter is incorrect. It is an array. I'm
printing it out. I can see it. What am I missing here?
<?php
$page_title = "My Little HTML Tag Function";
?>
<html>
<head>
<title><?php echo $page_title; ?></title>
</head>
<body>
<h1><?php echo $page_title; ?></h1>
<?php
/* Chapter 4-5: Functions and array Assignment.
By Jim Homme
Read valid HTML tags into an array from a file.
Call a function that wraps a piece of text in a tag.
Make sure the tag is valid.
*/
// Read tags from a file into an array.
$input = fopen("html_valid_tags.txt", "r");
if ($input) {
while (!feof($input)) {
$buffer = rtrim(fgets($input, 4096));
$tags[] = $buffer;
}
}
fclose($input);
print_r($tags); // It is an array, man.
/*
Function definition.
This function demonstrates how to use default function parameters.
*/
function tagThis($text = 'My default text.', $tag = 'p') {
/*
Paste the opening tag to the text, to the closing tag.
Check to see if it's a valid tag first.
This function isn't smart enough to tell tags that
are allowed to be empty like <img /> and br />
*/
if (in_array($tag, $tags)) { // Here's where it gets all upset.
return "<$tag>$text</$tag>";
} else {
echo "<p> Error. bad tag: $tag </p>"; // This should not be true.
}
}
?>
<p>
This is HTML unaffected by my tag wrapping function
</p>
<?php
$output = 'This is text I used in the function call.';
/* Use double quotes, so we don't have to use a
\ to escape single quotes.
*/
echo "<p>Now I'm going to print out the function output with default
parameters.</p>";
// We don't need quotes. Echo handles this.
echo tagThis();
echo '<p>Here is what the HTML Looks like.</p>';
/* You can put function calls inside each other.
This line escapes the HTML so the browser can show it.
*/
echo htmlentities(tagThis());
echo "<p>Here's the output wrapped in a 'pre' tag.</p>";
// Call the function with our variable and a tag.
echo tagThis($output, 'pre');
echo '<p>And the HTML:</p>';
// Now show the HTML again.
echo htmlentities(tagThis($output, 'pre'));
echo '<p>Cool, eh?</p>';
?>
<p>End of HTML</p>
</body>
</html>
James D Homme, Usability Engineering, Highmark Inc.,
james.homme@xxxxxxxxxxxx, 412-544-1810
"The difference between those who get what they wish for and those who
don't is action. Therefore, every action you take is a complete
success,regardless of the results." -- Jerrold Mundis
Highmark internal only: For usability and accessibility:
http://highwire.highmark.com/sites/iwov/hwt093/
__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind
Other related posts:
- » PHP Issue With in_array function