Link to home
Start Free TrialLog in
Avatar of markauk
markauk

asked on

Modifying content of web page on the fly

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",$command,$image);
      preg_match("/align=([^\s}]*)/i",$command,$align);
      preg_match('/caption.*"([^"]*)"/i',$command,$caption);
      $caption=stripslashes($caption[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,$replace,$buffer);

      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.
Avatar of abuzzuz
abuzzuz

What the hell is Moveable Type?
I'd like to help you but, as I said before, cant figure out exaclty what you are trying to achieve with this movable type and static html stuff.
Avatar of markauk

ASKER

MovableType is a blog/content management system (www.movabletype.org). The fact that I am using Movable Type doesn't really matter except to explain why I have individual pages which have content in them which needs modified on the fly (rather than having the pages done correctly when originally authored). As far as I know, I can't achieve what I want directly within Movable Type, hence needing separate php code to do it.
why dont you use BBcode (bulletin board)

eg [b]bold text[/b] [img=pic.jpg] some other normal text then [email=email@domain.com]email me[/email] the < character doesnt interfere with bbcode.

you then convert the code to html and store it for when you display the page. pointless converting on the fly. forums like phpBB use it.
You can also convert email addresses to code values, which bot dont pick up.

$encoded_email="";
for ($i = 0; $i < strlen($email); $i++) {
   $encoded_email.='&#'.ord(substr($email,$i,1));
}
Avatar of markauk

ASKER

Not sure I follow. BBcode is just html, but with [] instead of <>, right? I'd still have to parse through and convert [] to <>. phpBB presumably does this every time it outputs a page, and its pages are always generated dynamically on the fly. Also, the image insertion will be more complex than just inserting <img=pic.jpg> as I will also be inserting styles and other stuff I need to hide from the user.

I need to convert on the fly, because otherwise how do I change something that is sitting in a php file generated by moveable type to the code I want? (assuming movable type can't do the conversion itself, and I don't think it can)
ASKER CERTIFIED SOLUTION
Avatar of Fenric
Fenric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of markauk

ASKER

Thanks Fenric. That does the trick perfectly.