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

system.verbs.builtins.html.getOneTagValue

on getOneTagValue (htmltext, tagname) {
	<<given the html text, extract the value of a specific tag
		<<if you ask for "title" you'll get the text between <title> and </title>
		<<it handles tags that contain values, so you can use it to extract the <body> of a page
		<<Fri, Nov 29, 1996 at 12:29:12 PM by DW
	local (lowertext = string.lower (htmltext), tagval = htmltext);
	tagname = string.lower (tagname);
	
	on deleteUpTo (s) {
		local (ix);
		ix = string.patternmatch (s, lowertext);
		if ix == 0 {
			return (false)};
		tagval = string.delete (tagval, 1, ix);
		lowertext = string.delete (lowertext, 1, ix);
		return (true)};
	if not deleteUpTo ("<" + tagname) {
		return ("")};
	if not deleteUpTo (">") {
		return ("")};
	
	local (ix = string.patternmatch ("</" + tagname + ">", lowertext));
	if ix == 0 {
		return ("")};
	return (string.mid (tagval, 1, ix - 1))}
<<bundle <<test code
	<<dialog.alert ("*" + getOneTagValue ("xxx<Title val1 val2>hello!</title>yyy", "tiTle") + "*")
	<<dialog.alert ("*" + getOneTagValue ("<TItle>hello!</tiTle>", "tiTle") + "*")



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.