Monday, November 08, 2010 at 12:05 AM.

system.verbs.builtins.re.testing.replaceWithCallback

on iso8859Decode (s) {
	<<Does the reverse of string.iso8859Encode ().
		<<Converts HTML entities to ASCII chars in your machine's charset.
	
	on hexEncode (s) {
		local (result = "", i);
		for i = 1 to sizeof (s) {
			result = result + string.mid (string.hex (s [i]), 5, 2)};
		return (result)};
	
	on deEntify (adrTable, adrString) {
		local (key);
		key = hexEncode (adrTable^.matchString);
		if (defined (regex.examples.data.entities2ascii.[key])) {
			adrString^ = regex.examples.data.entities2ascii.[key]}
		else {
			adrString^ = adrTable^.matchString};
		return(true)};
	
	local (result = s);
	
	patternRef = re.compile ("\\[[^]]+\\]");
	result = re.replace (patternRef, "[xxx]", result, adrCallback:@deEntify);
	
	patternRef = re.compile ("&[a-zA-z]+;");
	result = re.replace (patternRef, "[xxx]", result, adrCallback:@deEntify);
	
	return(result)};

local (s = "ÊÍÇ[<][>]");
local (x);
x = s + " decodes to: " + iso8859Decode (s);
wp.newTextObject (x, @temp.text);
edit (@temp.text)



This listing is for code that runs in the OPML Editor environment. I created these listings because I wanted the search engines to index it, so that when I want to look up something in my codebase I don't have to use the much slower search functionality in my object database. Dave Winer.