-1

我有字符串,我需要从中获取开始和结束短语之间的子字符串。下面是我的字符串以及开始和结束短语。

我的字符串

安慰奖-卢比。8,000/-NN 189822 NO 189822 NP 189822 NR 189822 NS 189822 NU 189822 NU 189822 NV 189822 NW 189822 NX 189822 NX 189822 NY 189822 NZ 189822 NZ 189822 NZ 189822 2ND奖2ND-RS奖) NO 856109 (KOTTAYAM) NP 875729 (THRISSUR) NR 290878 (KOTTAYAM) NS 289830 (KOTTAYAM) NT 514156 (ALAPPUZHA) NU 178089 (PATHANAMTHITTA) NV 682950 (KOTTAYAM) NW 154659 (THRISSUR) NX 902063 (KANNUR) NY 747937 (IDUKKI ) NZ 393078 (KASARGODE) 以下列数字结尾的门票 4th Prize- Rs. 5,000/- 2427 3476 4343 5232 5805 6026 6028 6893 7321 7509 7682 9917 五等奖-卢比。1,000/ - 0084 0999 1043 1059 1617 1869 2351 3132 3209 3373 3972 3972 4109 4758 4955 5082 5649 5814 6219 6653 6800 7093 7093 7093 7325 7637 7637 7832 8368855555555555555555555555555555555555555555555555555555555999999999999999性范AANchinhin

开始短语: 安慰奖-卢比。8,000/-

结束语: 二等奖 - 卢比:500,000/-

预期结果: NN 189822 NO 189822 NP 189822 NR 189822 NS 189822 NU 189822 NV 189822 NW 189822 NX 189822 NY 189822 NZ 189822

我已经有一个小功能,但无法正常工作。:-)

     function getInbetweenStrings($start, $end, $str){
       $matches = array();
       $regex = "/$start([a-zA-Z0-9_]*)$end/";
       preg_match_all($regex, $str, $matches);
       return $matches[1];
    }

调用方法为

        $start = "Consolation Prize- Rs. 8,000\/-";
        $end = "2nd Prize- Rs :500,000\/-";
        $consold = $this->getInbetweenStrings($start, $end, $content1);
        print_r($consold);
4

2 回答 2

1

您刚刚错过了角色类中的空间。

$regex = "/$start([a-zA-Z0-9_ ]*)$end/";
#                    here ___^
于 2020-01-13T09:09:45.077 回答
0

请尝试运行以下代码


<?php

$str = "Consolation Prize- Rs. 8,000/- NN 189822 NO 189822 NP 189822 NR 189822 NS 189822 NU 189822 NV 189822 NW 189822 NX 189822 NY 189822 NZ 189822 2nd Prize- Rs :500,000/- NN 835117 (PALAKKAD) 3rd Prize- Rs :100,000/- NN 249043 (THRISSUR) NO 856109 (KOTTAYAM) NP 875729 (THRISSUR) NR 290878 (KOTTAYAM) NS 289830 (KOTTAYAM) NT 514156 (ALAPPUZHA) NU 178089 (PATHANAMTHITTA) NV 682950 (KOTTAYAM) NW 154659 (THRISSUR) NX 902063 (KANNUR) NY 747937 (IDUKKI) NZ 393078 (KASARGODE) FOR THE TICKETS ENDING WITH THE FOLLOWING NUMBERS 4th Prize- Rs. 5,000/- 2427 3476 4343 5232 5805 6026 6028 6893 7321 7509 7682 9917 5th Prize- Rs. 1,000/- 0084 0999 1043 1059 1617 1869 2351 3132 3209 3373 3972 4109 4758 4955 5082 5649 5814 6219 6653 6800 7093 7325 7637 7832 8368 8555 8610 8936 9678 9864 9994 6th Prize- Rs. 500/- 0290 0532 0609 0784 0846 0948 1128 1321 1361 1524 2259 3005 3019 3169 3231 3247 3282 3652 3766 3776 3785 4103 4226 4325 4388 4537 4596 4662 4671 4886 4967 5000 5119 5215 5333 5615 6196 6395 6639 6895 7378 7390 7642 7718 8069 8096 8141 8250 8550 8620 8634 8738 8761 8822 9031 9094 9134 9169 9829 9956 7th Prize- Rs. 100/- 0016 0032 0115 0137 0224 0235 0429 0442 0569 0579 0641 0878 0992 1019 1102 1129 1211 1255 1293 1301 1319 1470 1813 1854 1859 1877 2013 2132 2137 2147 2319 2380 2449 2629 2767 2840 2901 2984 2997 3055 3116 3325 3595 3657 3764 3770 3808 3828 3984 4058 4110 4251 4286 4531 4675 4696 4721 4795 4797 4824 5088 5210 5254 5285 5695 5744 5927 6038 6100 6319 6435 6438 6480 6594 6697 6734 6777 7227 7274 7296 7305 7329 7339 7561 7569 7670 7754 8059 8116 8175 8243 8258 8263 8521 8619 8751 8814 8949 8992 9282 9406 9434 9502 9568 9616 9619 9771 9940 The prize winners are advised to verify the winning numbers with the results published in the Kerala Government Gazatte and surrender the winning tickets within 30 days.";

$start_phrase = 'Consolation Prize- Rs. 8,000/-';
$end_pahrase = '2nd Prize- Rs :500,000/-';

$explode1 = explode($start_phrase, $str);

$explode1_data = $explode1[1];

$explode2 = explode($end_pahrase, $explode1_data);

echo $result = $explode2[0];

上面的代码经过测试和工作。

它会导致你

NN 189822 NO 189822 NP 189822 NR 189822 NS 189822 NU 189822 NV 189822 NW 189822 NX 189822 NY 189822 NZ 189822

我希望这能帮到您。

谢谢

于 2020-01-13T04:58:48.217 回答