0

I have some HTML that looks like this:

    $(document).ready(function(){
        $('#cumulative-returns').graph({
            width: 400,
            height: 180,
            type: 'bar',
            x_label: 'Month',
            x_data: ['Jan','Feb','Mar','Apr'],
            y_label: 'Cumulative Return',
            y_data: ['5','10','15','20'],
            colors: ['666666', '000000', 'ff0000', '333366']
        });

        $('#new-returns').graph({
            width: 400,
            height: 180,
            type: 'bar',
            x_label: 'Month',
            x_data: ['Jan','Feb','Mar','Apr'],
            y_label: 'Cumulative Return',
            y_data: ['5','10','15','20'],
            colors: ['666666', '000000', 'ff0000', '333366']
        });
    });

What I need to do is replace x_data and y_data with new values using some nifty regex in PHP.

This is what I have come up with so far just to find the right graph but even that doesn't work.

$graph = "cumulative-returns";
$start_tag = '$(\'#'.$graph.'\').graph({';
$end_tag = '});';
preg_match_all("/".preg_quote($start_tag)."(.+?)".preg_quote($end_tag)."/i", $html, $matches);
print_r($matches);

Any suggestions would be great!

*edit:

Please ignore the fact that its javascript, I just need to some regex to find the string between $('#cumulative-returns').graph({ and }); characters!

3
  • Thats javascript/jquery not html. Commented Jul 16, 2010 at 15:20
  • stackoverflow.com/questions/1732348/… Perhaps? Commented Jul 16, 2010 at 16:34
  • This seems like a bass-ackwards way of getting a value into your Javascript. Commented Jul 17, 2010 at 3:02

2 Answers 2

1
preg_match('/\$\(\'#cumulative-returns\'\)\.graph\({([^}]*)}\);/s',$input,$matches);

After this, $matches[0] will have the values matched by the regex (including the surrounding text) and $matches[1] will have just the text between $('#cumulative-returns').graph({ and }); characters.

Also, possibly more useful, would be:

preg_match_all('/\$\(\'#[^\']*\'\)\.graph\({([^}]*)}\);/s',$input,$matches);

After which matches[0][n] reflects the nth match (including surrounding text) and $matches[1][n] reflects the nth instance of text between $('#[any-text]').graph({ and });

Sign up to request clarification or add additional context in comments.

Comments

0

create a variable to hold the x_data data Then just manipulate that variable ...

var myvar = ['dec', 'may', 'whatever', 'whatver' ]
    $('#cumulative-returns').graph({
                width: 400,
                height: 180,
                type: 'bar',
                x_label: 'Month',
                x_data: myvar,
                y_label: 'Cumulative Return',
                y_data: ['5','10','15','20'],
                colors: ['666666', '000000', 'ff0000', '333366']
            });

1 Comment

No please ignore the fact that its javascript, I just need to some regex to find the string between $('#cumulative-returns').graph({ and }); characters!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.