亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在php中獲取具有匹配文本的所有行

如何在php中獲取具有匹配文本的所有行

PHP
阿晨1998 2022-08-19 16:46:32
我有一個字符串供練習。$message = "Hurray! You've received a $100 Amazon Gift Card. Hope you enjoy this Amazon Gift Card! What's next? Apply the gift card to your Amazon account:=20 https://www.amazon.com/g/ Don't have an Amazon account? Sign up to redeem: www.amazon.com You can also redeem your gift card at checkout using this claim code: NE7N-TUD6NV-GUAB We hope to see you again soon, Amazon.com Once applied to your Amazon account, the entire amount will be added to you= r gift card balance. Your gift card balance can't be transferred to other a= ccounts, used to buy other gift cards, or, except as required by law, redee= med for cash. Your gift card balance will be applied automatically to eligible orders dur= ing the checkout process and when using 1-Click. If you don=E2=80=99t want = to use your gift card balance on your order, you can unselect it as a payme= nt method in checkout.=20 If you experience any issues using your gift card, you can reference your g= ift card by providing the following information to Customer Service: Order Number: 234343433433";我想使用preg_match獲取索賠代碼,但我失敗了。這是我的代碼。 if(preg_match_all("/claim code:.*\s/", $message, $array)){             print_r($array);        }輸出是這個Array ( [0] => Array ( [0] => claim code: ) )但我希望它顯示索賠代碼,任何人都可以幫忙嗎?
查看完整描述

2 回答

?
MM們

TA貢獻1886條經驗 獲得超2個贊

我會使用這個模式:(?<=\bclaim code: )\S+


$message = "Hurray! You've received a $100 Amazon Gift Card. Hope you enjoy this Amazon Gift Card! What's next? Apply the gift card to your Amazon account:=20 https://www.amazon.com/g/ Don't have an Amazon account? Sign up to redeem: www.amazon.com You can also redeem your gift card at checkout using this claim code: NE7N-TUD6NV-GUAB We hope to see you again soon, Amazon.com Once applied to your Amazon account, the entire amount will be added to you= r gift card balance. Your gift card balance can't be transferred to other a= ccounts, used to buy other gift cards, or, except as required by law, redee= med for cash. Your gift card balance will be applied automatically to eligible orders dur= ing the checkout process and when using 1-Click. If you don=E2=80=99t want = to use your gift card balance on your order, you can unselect it as a payme= nt method in checkout.=20 If you experience any issues using your gift card, you can reference your g= ift card by providing the following information to Customer Service: Order Number: 234343433433";

preg_match_all("/(?<=\bclaim code: )\S+/", $message, $matches);

print_r($matches[0][0]);

這打?。?/p>


NE7N-TUD6NV-GUAB

您當前方法的主要問題是正則表達式模式本身。 匹配零個或多個空格字符,您應該像我上面使用的那樣使用。\s*\S*


查看完整回答
反對 回復 2022-08-19
?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

你可以使用積極的觀察來實現這個目標。我建議使用,除非你有多個索賠代碼。因為內存消耗比 .對于您的解決方案,請查看以下代碼:preg_matchpreg_match_all()preg_match()


$message = "Hurray! You've received a $100 Amazon Gift Card. Hope you enjoy this Amazon Gift Card! What's next? Apply the gift card to your Amazon account:=20 https://www.amazon.com/g/ Don't have an Amazon account? Sign up to redeem: www.amazon.com You can also redeem your gift card at checkout using this claim code: NE7N-TUD6NV-GUAB We hope to see you again soon, Amazon.com Once applied to your Amazon account, the entire amount will be added to you= r gift card balance. Your gift card balance can't be transferred to other a= ccounts, used to buy other gift cards, or, except as required by law, redee= med for cash. Your gift card balance will be applied automatically to eligible orders dur= ing the checkout process and when using 1-Click. If you don=E2=80=99t want = to use your gift card balance on your order, you can unselect it as a payme= nt method in checkout.=20 If you experience any issues using your gift card, you can reference your g= ift card by providing the following information to Customer Service: Order Number: 234343433433";


preg_match('/(?<=\bclaim\scode\b:\s)\S+/', $message, $matches);

echo $matches[0]; // NE7N-TUD6NV-GUAB

正面觀察:


確保給定的模式匹配,在表達式中的當前位置結束。圖案必須具有固定的寬度。不消耗任何字符。


語法:


(?<=...)


查看完整回答
反對 回復 2022-08-19
  • 2 回答
  • 0 關注
  • 239 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號