Warning: Call-time pass-by-reference has been deprecated - argument passed by value; If you would like to pass it by reference, modify the declaration of method_exists(). If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file. However, future versions may not support this any longer. in /var/www/vhosts/oldschool.com.sg/fc_pub/modules/Shared/actions/StaticAction.class.php on line 16
ASCIIMathPHP - PHP port of ASCIIMath
Traditional Standards, New Methods

ASCIIMathPHP - PHP port of ASCIIMath

Welcome to the ASCIIMathPHP Project Page! ASCIIMathPHP is a PHP port of ASCIIMath, a really great Javascript application to render ASCII forumlas as MathML. The ASCIIMathPHP Project is supported by Old School. Try out the ASCIMathPHP Sandbox.

Latest Version

ASCIIMathPHP 2.0 (for PHP5 only). With the end of life announcement of PHP4, I have developed a PHP5 only version of ASCIIMathPHP. Same functionality as ASCIIMathPHP 1.12.1.

ASCIIMathPHP 1.12.1 (for PHP4 only). This is a quick release to 1.12 that fixes the setCurrExpr() missing method bug. If you have downloaded 1.12, please use 1.12.1 instead.

Changelog

Version Changes
2.0
  • PHP5 only version of ASCIIMathPHP
1.12.1
  • Included the missing setCurrExpr() method
1.12
  • Added changes that David Lippman (DLippman at pierce dot ctc dot edu) made to bring ASCIIMathPHP up to ASCIIMath 1.4.7 functionality
  • Added parseIntExpr, for intermediate expression parsing rule, allowing x^2/x^3 to render as (x^2)/(x^3)
  • Added quotes as another way of designating text; "hello" is equivalent to text(hello)
  • Added FUNC designator to allow sin, cos, etc to act as functions, so sin x /x renders as {sin x}/x
1.11
  • Fixed bug that stopped script execution for incomplete expressions
  • Changed the algorithm for parsing expressions so that it matches the longest string possible (greedy)
1.10
  • Added definition support
  • Added stackrel support
  • Added a bunch of different symbols etc. >>, << and definitions like dx, dy, dz etc.
  • This update is courtesy of Wes Cowley at Cowlix.com
1.02
  • Fixed bug with mbox and text
  • Fixed spacing bug with mbox and text
1.01
  • Fixed Bug that did not parse symbols greater than a single character correctly when appearing at end of expression.

Code Examples

There are 2 parts to getting ASCIIMathPHP to work. The first, is to get the actual ASCIIMathPHP class working and generating MathML. The next, is to make sure the brower understands that it is actually seeing MathML so it can render it properly.

Generating MathML

include('ASCIIMathPHP.cfg.php');
include('ASCIIMathPHP.class.php');

$ascii_math = new ASCIIMathPHP($symbol_arr); // The $symbol array is defined in ASCIIMathPHP.class.php
$ascii_math->setExpr($expr); // The ASCIIMath expression you want generate MathML for
$ascii_math->genMathML();

print($ascii_math->getMathML());

Use the code above, to generate MathML presentation markup from ASCIIMath syntax. Once the ASCIIMathPHP object has been instantiated, continue using the code below to parse more ASCIIMath expressions.

$ascii_math->setExpr($expr);
$ascii_math->genMathML();

$mathml = $ascii_math->getMathML();

An alternative would be to create a callback function and use it to parse an entire page of expressions. Let's say you are using backquotes(`) as delimters.

function ASCIIMathPHPCallback($mtch_arr)
{
	$txt = trim($mtch_arr[1]);
	
	include('ASCIIMathPHP.cfg.php');
	require_once('ASCIIMathPHP.class.php');

	static $asciimath;
	
	if (!isset($asciimath)) $asciimath = new ASCIIMathPHP($symbol_arr);
		
	$math_attr_arr = array('displaystyle' => 'true');
		
	$asciimath->setExpr($txt);
	$asciimath->genMathML($math_attr_arr);
	
	return($asciimath->getMathML());
}

Once you have created the callback function, use the following code to parse a string with a lot of embedded ASCIMath expressions.

$regexp = '/`(.*?)`/s'; // Backquotes as delimiters
$txt_with_mathml = preg_replace_callback($regexp,'ASCIIMathPHPCallback',$txt_with_asciimath_exprs);

echo $txt_with_mathml;

Getting the Browser to render MathML

Currently, I only know of 3 browsers which can render MathML well. Internet Explorer 6 or 7 with MathPlayer and Mozilla Firefox. Safari and Opera do not support MathML at the moment. MathML can be embedded in XML or HTML. This site uses the HTML method. Programtically, XML is easier to implement but it puts a lot of restrictions about what else you can put in the webpage. It also requires a separate XSL stylesheet, pmathml.xsl. In the XML document header you must have:

<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?xml-stylesheet type="text/xsl" href="pmathml.xsl"?>
<!-- 
************************************************************************
If you see this text while using Internet Explorer, please install the 
free MathPlayer plugin from http://www.dessci.com/en/products/mathplayer/
This will enable your browser to display properly formatted mathematics.
************************************************************************
-->
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:mml="http://www.w3.org/1998/Math/MathML">

Please remember that you have to output the header('Content-type: text/xml') HTTP header as well to get the browser to understand that it is seeing XML. HTML is more complicated, requires Javascript and requires the use of specific XML namespaces but is much more flexible. Your HTML documents should have the following header:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:m="http://www.w3.org/1998/Math/MathML">
<head>
<title>Some Title</title>
<object id="mathplayer" classid="clsid:32F66A20-7614-11D4-BD11-00104BD3F987">
</object><?import namespace="m" implementation="#mathplayer"?>
<script type="text/javascript" src="htmlMathML.js"></script>
</head>
<body onload="convert();">

htmlMathML.js is very important and jumpstarts Firefox's Gecko Engine to render MathML embedded in HTML correctly. You also need to surround each <math>…</math> with <span class="asciimath"></span> tags. This is critical for the JS function to work.

That's about it! If you have any questions, just use the Inquiry Form.

Copyright © 2005, 2006, 2007, 2008. Old School. Best viewed in 1024 x 768 with Firefox 1.0 or IE 6.

Would you like to sponsor Old School? Contact Us!