PHP BB to XHTML (Break XHTML, win a prize!)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

PHP BB to XHTML (Break XHTML, win a prize!)

Post by JAB Creations »

I found the original base code somewhere so I'm not claiming any credit for this code. However I am interested to see if anyone can break the XHTML code! The prize is seeing me struggle to implement a fix. :mrgreen:

The page is served as true XHTML (application/xhtml+xml) so if the XML breaks the whole page does. For obvious reasons this can't effectively be tested in Internet Explorer.

Thoughts please?

<?php
$mime = (stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml")) ? "application/xhtml+xml" : "text/html";
header("content-type:$mime;");
echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>PHP BB Code Test</title>
<style type="text/css">
span.b {font-weight: bold;}
span.c {color: #f00;}
span.i {font-style: italic;}
span.s {text-decoration: line-through;}
span.u {text-decoration: underline;}
</style>
</head>

<body>

<h1>PHP BB to XHTML Test</h1>
<div>
<?php
$output="[b]this text is bold[/b]
[syntax=php]this text is code[/syntax]
[i]this text is italic[/i]
[u]this text is underlined[/u]
[s]this text is striked[/s]
http://www.example.com
http://www.example.com
[link]http://www.example.com[/link]";

$output=preg_replace("'\[link\](.*?)\[/link\]'i","\\1",$output);

$find = array(
"/(^|\W)(http:\/\/[\w\.\-\?\/\=\&]+)/siu",
"'\[b\](.*?)\[/b\]'is",
"'\[code\](.*?)\[/code\]'is",
"'\[i\](.*?)\[/i\]'is",
"'\[s\](.*?)\[/s\]'is",
"'\[u\](.*?)\[/u\]'is"
);

$replace = array(
'\\1<a href="\\2" target="_blank">\\2</a>',
'<span class="b">\\1</span>',
'<span class="c">\\1</span>',
'<span class="i">\\1</span>',
'<span class="s">\\1</span>',
'<span class="u">\\1</span>'
);

$output=preg_replace($find,$replace,$output);
//echo $output;

echo nl2br($output);
?>

</div>

</body>
</html>
Post Reply