将这些结果拆分为数组

发布于 2024-12-12 02:50:21 字数 1274 浏览 0 评论 0原文

我真的很难制作一些正则表达式来将:

Child Non Peak @ $10.75 USD <br/>
<input name="quan0" maxlength="1" size="1" class="star_N"/> <br>
Infant Non Peak @ $13.75 USD <br/>
<input name="quan1" maxlength="1" size="1" class="star_N"/> <br>
Adult Non Peak @ $13.75 USD <br/>
<input name="quan2" maxlength="1" size="1" class="star_N"/> <br>
Senior Non Peak @ $12.50 USD <br/>
<input name="quan3" maxlength="1" size="1" class="star_N"/> <br>
Member Adult Non Peak @ $12.75 USD <br/>
<input name="quan4" maxlength="1" size="1" class="star_N"/> <br>
Member Senior Non Peak @ $11.50 USD <br/>
<input name="quan5" maxlength="1" size="1" class="star_N"/> <br>
Member Child Non Peak @ $9.75 USD <br/>
<input name="quan6" maxlength="1" size="1" class="star_N"/> <br>

变成一个数组,就像

array (
 value: Child Non Peak @ $10.75 USD 
 price: 10.75

有人可以帮忙吗?我已经有了这段非常复杂的正则表达式,但它不适合目的。

preg_match_all('/class="t12">(?P<type>\w+)[\s@]*(?P<cost>\$[\d.]*)\s+USD(.*)name="(?P<input>quan\w+)"/msU',
               $output,
               $matches,
               PREG_SET_ORDER);

I'm really struggling to make some regex to turn:

Child Non Peak @ $10.75 USD <br/>
<input name="quan0" maxlength="1" size="1" class="star_N"/> <br>
Infant Non Peak @ $13.75 USD <br/>
<input name="quan1" maxlength="1" size="1" class="star_N"/> <br>
Adult Non Peak @ $13.75 USD <br/>
<input name="quan2" maxlength="1" size="1" class="star_N"/> <br>
Senior Non Peak @ $12.50 USD <br/>
<input name="quan3" maxlength="1" size="1" class="star_N"/> <br>
Member Adult Non Peak @ $12.75 USD <br/>
<input name="quan4" maxlength="1" size="1" class="star_N"/> <br>
Member Senior Non Peak @ $11.50 USD <br/>
<input name="quan5" maxlength="1" size="1" class="star_N"/> <br>
Member Child Non Peak @ $9.75 USD <br/>
<input name="quan6" maxlength="1" size="1" class="star_N"/> <br>

into an array thats like

array (
 value: Child Non Peak @ $10.75 USD 
 price: 10.75

Could anyone assist? I have this largely complex piece of regex already but its not fit for purpose.

preg_match_all('/class="t12">(?P<type>\w+)[\s@]*(?P<cost>\$[\d.]*)\s+USD(.*)name="(?P<input>quan\w+)"/msU',
               $output,
               $matches,
               PREG_SET_ORDER);

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

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

发布评论

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

评论(3

稚气少女 2024-12-19 02:50:22
preg_match_all('/[\w ]* @ \$?([0-9]{1,}\.?[0-9]*) [A-Z]{2,3} ?/', $output, $matches, PREG_SET_ORDER);

print_r( $matches );

给出了下一个结果:

Array
(
    [0] => Array
        (
            [0] => Child Non Peak @ $10.75 USD 
            [1] => 10.75
        )
    [1] => Array
        (
            [0] => Infant Non Peak @ $13.75 USD 
            [1] => 13.75
        )
    [2] => Array
        (
            [0] => Adult Non Peak @ $13.75 USD 
            [1] => 13.75
        )
    [3] => Array
        (
            [0] => Senior Non Peak @ $12.50 USD 
            [1] => 12.50
        )
    [4] => Array
        (
            [0] => Member Adult Non Peak @ $12.75 USD 
            [1] => 12.75
        )
    [5] => Array
        (
            [0] => Member Senior Non Peak @ $11.50 USD 
            [1] => 11.50
        )
   [6] => Array
        (
            [0] => Member Child Non Peak @ $9.75 USD 
            [1] => 9.75
        )
)
preg_match_all('/[\w ]* @ \$?([0-9]{1,}\.?[0-9]*) [A-Z]{2,3} ?/', $output, $matches, PREG_SET_ORDER);

print_r( $matches );

gave the next result:

Array
(
    [0] => Array
        (
            [0] => Child Non Peak @ $10.75 USD 
            [1] => 10.75
        )
    [1] => Array
        (
            [0] => Infant Non Peak @ $13.75 USD 
            [1] => 13.75
        )
    [2] => Array
        (
            [0] => Adult Non Peak @ $13.75 USD 
            [1] => 13.75
        )
    [3] => Array
        (
            [0] => Senior Non Peak @ $12.50 USD 
            [1] => 12.50
        )
    [4] => Array
        (
            [0] => Member Adult Non Peak @ $12.75 USD 
            [1] => 12.75
        )
    [5] => Array
        (
            [0] => Member Senior Non Peak @ $11.50 USD 
            [1] => 11.50
        )
   [6] => Array
        (
            [0] => Member Child Non Peak @ $9.75 USD 
            [1] => 9.75
        )
)
你不是我要的菜∠ 2024-12-19 02:50:22

([a-zA-Z ]+ @ \$(\d+\.\d{2}) USD) 应该会给你很好的结果

  • [a-zA-Z ]+< /code> 是产品名称,
  • @ 位于产品和价格之间
  • \$(\d+\.\d{2}) 价格,group 捕获至少 1 位数字,后跟 .后跟 2 位数字
  • USD 捕获最后一个空格和货币参考

([a-zA-Z ]+ @ \$(\d+\.\d{2}) USD) should probably give you good results

  • [a-zA-Z ]+ is the product name,
  • @ is between product and the price
  • \$(\d+\.\d{2}) price, group captures atleast 1 digit followed by a . followed by 2 digits
  • USD captures the last space and currency reference
温柔女人霸气范 2024-12-19 02:50:22

使用像 DOMDocument 这样的 DOM 解析器来检查元素。

使用正则表达式解析 HTML 可能会导致意想不到的后果

Use a DOM parser like DOMDocument to inspect the elements.

Using regex to parse HTML may lead to unintended consequences.

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