0

I'm re-designing a mathematics program for students. The original program was written in adobe flash. This time I am doing it in PHP and JavaScript.

In the original program there are several XML files with hundreds of mathematical questions and some of which are fractions. Those fractions are written as: #(1,4). I want to make an function called # which can make classic fractions.

The problem: there is normal text around the #(1,3) and it is not separated with quotations. How can I let the browser know there is a function in the text? Example:

  <question>what is #(2,8) divided by #(3,4)</question>

I hope you know how to do this. Thanks.

mike
  • 4,929
  • 4
  • 40
  • 80
Mark
  • 3,224
  • 2
  • 22
  • 30

4 Answers4

1

Without more detailed information:

var s = "<question>what is #(2,8) divided by #(3,4)</question>";
var t = s.replace(/(^|\s|>)#\((\d+),(\d+)\)($|\s|<)/g, "$1<span class='fraction'><span class='numerator'>$2</span><span class='denominator'>$3</span></span>$4");

t is now

"<question>what is <span class='fraction'><span class='numerator'>2</span><span class='denominator'>8</span></span> divided by <span class='fraction'><span class='numerator'>3</span><span class='denominator'>4</span></span></question>"

So: regular expression where you find #(...,...) where the ... are numbers, and the "function" strings are either surrounded by white space, the start/end of the sentence, or angle brackets.

However, rewriting it to MathJax MathML or LaTeX format (http://mathjax.org) instead will look much nicer.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
0

Regular expression!

^#\([\d]+,[\d]+\)$

This will match anything that begins/ends with #([*number(s)*],[*number(s)*])

You can have PHP do this and format however you need it.

Dakotah Hicock
  • 380
  • 2
  • 15
0

You could parse the text in PHP, using preg_replace(). Here's an example of how it might work:

preg_replace( "/#\((\d+),(\d+)\)/" , "$1/$2" , $question );

You'd want to write a loop to get each question from the xml and run this line of code on it.

This will take a line like this:

what is #(2,8) divided by #(3,4)

And give you this:

what is 2/8 divided by 3/4

You can tinker with this code and see it work in the PHP Sandbox.

Surreal Dreams
  • 26,055
  • 3
  • 46
  • 61
0

As you pointed out... there is a link treating part of your problem already.

How to display "Classic" built-up fractions with an horizontal line in CSS / JavaScript?

I worked-around the jsfiddle of the link to satisfy your needs :

http://jsfiddle.net/xW7d8/156/

The only change resides in the javascript, changing your #(1,2) to 1/2. And then the existing script does the job.

$('.fraction').each(function(key, value) {
    $this = $(this) ;
    var s = $this.text() ;
    var n = s.replace(/#\((\d+),(\d+)\)/g, "$1/$2") ;
    var split = n.split("/")
    if( split.length == 2 ){
        $this.html('<span class="top">'+split[0]+'</span><span class="bottom">'+split[1]+'</span>')
    }    
});

-- Edit -- Does not take into account the change from <question> String </question> to <span ...> String </span> though. You could use a combination of the jsfiddle and the accepted answer :)

Community
  • 1
  • 1
Adrien Gorrell
  • 1,291
  • 3
  • 14
  • 28