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

发布于 2024-12-04 09:49:01 字数 698 浏览 1 评论 0原文

这是需要 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

您建议我如何做到这一点?

Here is the text that needs to be 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'};

I would like to be able to grab the following from the text above:

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

How do you propose I can do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝礼 2024-12-11 09:49:01

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

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

Regex newbie here, got something with that:

/\((.+?,){25}\"(.+?)\".+?'L':'(.+?)'/
悲喜皆因你 2024-12-11 09:49:01

我是这样解决的:

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);

Here is how I solved it:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文