Scenario:
Users enter content which goes on a web site using Movable Type. Users are non-technical, but I need to enable a couple of things without requiring users to enter html, code etc. Specifically:
(1) catch any email addresses entered and print them in a format not readable by spambots
(2) allow easy insertion of images within the text
Planned solution:
Movable Type will output a static file for each entry based on a predefined template. This template will contain PHP code to provide the functionality I require. Use ob_start to route all output to a function which
(1) routes any email addresses found to a function (already written and working, uses javascript to 'mangle' addresses in a non machine readable format)
(2) looks for any instances of {image=file.jpg} and replaces with the relevant IMG html
The code I have so far is as follows:-
<?
//load email() function
require("dev/inc/email.inc
");
function image($command) {
//convert {image=[image filename] align=[left|right|center] caption=[caption in quotes]} to correct html
preg_match("/image=([^\s}]
*)/i",$com
mand,$imag
e);
preg_match("/align=([^\s}]
*)/i",$com
mand,$alig
n);
preg_match('/caption.*"([^
"]*)"/i',$
command,$c
aption);
$caption=stripslashes($cap
tion[1]);
//output correct html for IMG (or dummy text for testing)
echo("IMAGE=$image[1] ALIGN=$align[1] CAPTION=$caption");
}
function callback($buffer) {
//regexp for replacing emails with email() function
$search[0]="/[_a-z0-9-]+(\
.[_a-z0-9-
]+)*@[a-z0
-9-]+(\.[a
-z0-9-]{2,
})+/ie";
$replace[0]="email('\\0','
\\0');";
//regexp for replacing {image} with IMG html code by calling image() function
$search[1]="/{image[^}]*}/
ie";
$replace[1]="image('\\0');
";
$buffer = preg_replace($search,$repl
ace,$buffe
r);
return($buffer);
}
?>
<html>
<body>
<p><b>Callback function called directly</b></p>
<?echo(callback('<p>An image might go here {image=img.jpg caption="hello there" align=right}</p><p>Just email me at email@somewhere.com</p>'))
;?>
<p><b>Callback function called through ob_start</b></p>
<?ob_start("callback");?>
<p>Just email me at email@somewhere.com</p>
<p>An image might go here {image=img.jpg align=right caption=hello there}</p>
<? ob_end_flush();?>
</body>
</html>
Problems:
(1) when calling the callback function directly (ie not through ob_start), the results of the replacements are output first, then the rest of the text.
(2) when calling through ob_start, no replacement results are output, just the buffer less the bits that matched.
Requirement:
Either help me to get this code working correctly, or suggest a better way to achieve the same. Note that over time I will probably add more replacements, so it needs to be flexible enough to accept an arbitrary number of replacements.
Thanks
mark.
Start Free Trial