0

Possible Duplicate:
PHP Regex extract a javascript variable

This is really probably just a simple string manipulation question that needs a good regex solution.

I have some javascript variables defined in an HTML page. I want to read the page in to PHP, identify the variable I want to change; and update it. The vars look like:

rsp.pageId = 'sports'

Just to be clear, my script will not know what the value is so in the example 'sports' could be anything; so it's a wildcard where the rest of the string is consistant. I don't know if it would be easier to wrap some comments around the var/value to get to it easier?

I appreciate the help. I'm playing with a DOM manipulation library so I thought about wrapping script tags around each variable and putting an ID on each. I think that would work but seems like the output would be a bit ridiculous.

Community
  • 1
  • 1
Ecropolis
  • 2,005
  • 2
  • 22
  • 27
  • possible duplicate of [PHP Regex extract a javascript variable](http://stackoverflow.com/questions/11378690/php-regex-extract-a-javascript-variable) or http://stackoverflow.com/questions/5951395/extracting-javascript-variable-values-via-web-scraping or http://stackoverflow.com/questions/10621940/how-find-value-of-javascript-variable-with-regex – mario Oct 25 '12 at 21:04
  • really? the first two answers aren't related. why not edit them out. – kalpaitch Oct 25 '12 at 21:17

1 Answers1

0

do you need the spaces around the equals, if not here - preg_replace( "/rsp\.pageId='([a-zA-Z0-9])+'/", "rsp.pageId='blah'" ); if so try here - preg_replace( "/rsp\.pageId\s?=\s?'([a-zA-Z0-9])+'/", "rsp.pageId='blah'" );

kalpaitch
  • 5,193
  • 10
  • 43
  • 67