正则表达式使用preg_match获取内容

时间:2011-09-13 06:05:21

标签: php regex preg-match

以下是需要preg_matched的文本。

/* * product 2: * catalogServiceType = 2 * catalogCacheType = 1 * catalog2RemoteCacheActive = false */ var objProduct881705 = new ProductStyle("2","881705","","4149367","247",false,false,false,false,'','',"35401",false,'$79.50',true,false,false,false,0,false,true,false,true,99,5,"First content that needs to be matched",'Color',"","Machine.","","false","false","false",0,0,"C1","1","",true);obj={'S':'http://domainname.com/mini/image.jpg', 'L':'http://domainname.com/main/image.jpg'};

我希望能够从上面的文字中获取以下内容:

1. First content that needs to be matched
2. http://domainname.com/main/image.jpg

你怎么建议我能做到这一点?

2 个答案:

答案 0 :(得分:0)

正则表达式新手在这里得到了一些东西:

/\((.+?,){25}\"(.+?)\".+?'L':'(.+?)'/

答案 1 :(得分:0)

以下是我解决它的方法:

preg_match('/new ProductStyle\(([^\)]+)\)/', $html, $matches);
$jsonValues = '[' . $matches[1] . ']';
$jsonValues = preg_replace_callback("/(?<=,)'(.*?)'(?=,)/", create_function(
      '$matches',
      'return \'"\' . addslashes(strip_tags($matches[1])) . \'"\';'
    ), $jsonValues);
$values = json_decode($jsonValues);