2014-08-27 79 views
0

我的示例代码如下。如何从另一个字符串提取字符串之间的第N和N + 1'破解'在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!"; 

从上面的字符串我想要得到第二个和第三个'breakword'单词之间的句子。 这意味着我想提取'如果我有这个解决方案,我会很高兴'。 如果您有任何解决方案,请帮助我。预先感谢您

回答

3

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

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

PHP> 5.4将支持这个太:

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

旁注:其实5.4支持在 – Ghost 2014-08-27 08:00:29

+0

谢谢它的工作职能解除引用。 – 2014-08-27 08:01:25

+0

可否请你给我提供代码来计算该字符串中的破解号码的号码 – 2014-08-27 08:10:20

1

了类似的做法,但这一翻译使用explode的你也可以使用preg_split

支持(PHP 4, PHP 5)

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

+0

是的,它也在工作 – 2014-08-27 08:14:36

+0

你可以有代码来计算'该字符串中的分词'的号码 – 2014-08-27 08:15:14

+1

@ AnilBhattarai100'substr_count()' – Ghost 2014-08-27 08:16:38

1

第二个答案,你可以使用

$dummy=array(); 
preg_match_all('/breakword/', $a,$dummy) 
相关问题