Click to See Complete Forum and Search --> : More questions of showing code (NogDog, please read)


Mr Initial Man
01-04-2006, 09:12 AM
This is the PHP code I have thus far:

<?php
/*******************************************************
* page for showing markup of refering page
*******************************************************/
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
<head>
<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>
<title>Page title</title>
<link type="text/css" rel="stylesheet" href="coding.css">
</head>
<body>
<?php
if(!empty($_SERVER['HTTP_REFERER']))
{
$file = $_SERVER['HTTP_REFERER'];
$text = @file_get_contents($file);
echo "<h1>Markup for $file</h1>\n";
if($text !== FALSE)
{
echo "<code>" . nl2br(htmlentities($text)) . "</code>\n";
}
else
{
echo "<p class='error'>ERROR: Unable to read file $file.</p>\n";
}
}
else
{
echo "<p class='error'>ERROR: No http_referer was received.</p>\n";
}
?>
</body>
</html>

What I would like to know is if there is some php that would highlight element names and attributes in the page, so instead of <html lang="en" dir="ltr"> it would look like <html lang="en" dir="ltr">. Is this possible with PHP?

NogDog
01-04-2006, 02:41 PM
Yes, it could be done, probably using preg_replace(). It is not a trivial thing though - I'll think about it later when I have some time. :)

NogDog
01-04-2006, 09:52 PM
<?php
/*******************************************************
* page for showing markup of refering page
*******************************************************/
function addColor($text)
{
return(preg_replace('/&lt;(\s*\S+)(\s.*){0,1}&gt;/U',
"<strong>&lt;<span style='color:#900'>$1</span>".
"<span style='color:#090'>$2</span>&gt;</strong>",
$text)
);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
<head>
<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>
<title>Page title</title>
<link type="text/css" rel="stylesheet" href="coding.css">
</head>
<body>
<?php
if(!empty($_SERVER['HTTP_REFERER']))
{
$file = $_SERVER['HTTP_REFERER'];
$text = @file_get_contents($file);
echo "<h1>Markup for $file</h1>\n";
if($text !== FALSE)
{
echo "<code>" . addColor(nl2br(htmlentities($text))) . "</code>\n";
}
else
{
echo "<p class='error'>ERROR: Unable to read file $file.</p>\n";
}
}
else
{
echo "<p class='error'>ERROR: No http_referer was received.</p>\n";
}
?>
</body>
</html>

Mr Initial Man
01-05-2006, 01:00 AM
Works great! Thank you!