如何从PHP中的第N个和第N + 1个'breakword'之间的另一个字符串中提取字符串?

时间:2014-08-27 07:53:24

标签: php

我的示例代码如下所示。

$a= "I think there are many people who can help in my difficulty. breakword Thank you in advance. brwakword If i got this solution I will be happy. Breakword I have already searched in google about it I did not get it's solution. Breakword If you have any link of solution please help me. breakword Thank you again!";

从上面的字符串中我想得到第2和第3个'breakword'字样之间的句子。 这意味着我想提取'如果我得到这个解决方案,我会很高兴。' 如果您有任何解决方案,请帮助我。提前谢谢

3 个答案:

答案 0 :(得分:3)

您可以使用explode()将字符串拆分为更小的部分,并获取数组的“第N”部分。

$sentences = explode('breakword', $a);
echo $sentences[2];

PHP> 5.4也将支持这一点:

echo explode('breakword', $a)[2];

答案 1 :(得分:1)

类似的方法,但您也可以使用explode

而不是preg_split

支持(PHP 4, PHP 5)

preg_split('/breakword/', $a);

答案 2 :(得分:1)

您可以使用第二个答案

$dummy=array();
preg_match_all('/breakword/', $a,$dummy)