Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取两个单词之间的内容_Php - Fatal编程技术网

Php 获取两个单词之间的内容

Php 获取两个单词之间的内容,php,Php,我想得到两个单词(word1和word2)之间的内容(文本),例如: word1ballabla诗歌可分为几种类型。word2 blabla 因此,内容=bla诗歌可以分为几个体裁或类别。 但是,word1和word2有时会出现大写字母或小写字母的问题。另一个问题是,word1仍然会打印在结果中。应该打印出来。如何处理这个问题?谢谢:) 代码如下: $file = 'word1 blablabla word1 Poetry can be divided into several genres,

我想得到两个单词(
word1
word2
)之间的内容(文本),例如:

word1ballabla诗歌可分为几种类型。word2 blabla

因此,内容=
bla诗歌可以分为几个体裁或类别。

但是,
word1
word2
有时会出现大写字母或小写字母的问题。另一个问题是,
word1
仍然会打印在结果中。应该打印出来。如何处理这个问题?谢谢:)

代码如下:

$file = 'word1 blablabla word1 Poetry can be divided into several genres, or categories. word2 blablabla ';
$word1='word1';
$word2='word2';
$between  = substr($file, strpos($file, $word1), strpos($file, $word2) - strpos($file, $word1));
您可以使用而不是
strpos

stripos-查找第一个匹配项的位置 字符串中不区分大小写的子字符串

您可以使用而不是
strpos

stripos-查找第一个匹配项的位置 字符串中不区分大小写的子字符串


哦,谢谢你!另一个问题是,
word1
仍然会打印在结果中。
stripos()
返回第一个单词开始的位置。。。因此,要排除它,请将其长度添加到结果的开始偏移量中:
substr($file,stripos($file,$word1)+strlen($word1),…)
@corexi
word1
由5个字母组成。所以我应该是
substr($file,(stripos($file,$word1)+5)
对吗?cmiiwIt总是使用动态代码,比如
strlen($word1)
,而不是在应用程序中使用硬核魔法数字。你会稍后回来问“为什么这是5?”@CoreXii噢,我真傻,我没有仔细阅读你对strlen的第一条评论。是的,我明白了,谢谢!噢,谢谢..!另一个问题是,
word1
仍然会打印在结果中。
stripos()
返回第一个单词开始的位置…因此,要排除它,请将其长度添加到结果的开始偏移量:
substr($file,stripos($file,$word1)+strlen($word1),…)
@corexi
word1
由5个字母组成。因此我应该是
substr($file,(stripos($file,$word1)+5)
正确吗?cmiiwIt最好使用动态代码,比如
strlen($word1)
,而不是在应用程序中使用硬核魔术数字。你稍后会回来问“为什么这里是5?”@CoreXii噢,我真傻,我没有仔细阅读你关于strlen的第一条评论。是的,我明白了,谢谢你!@RegisteredUser为什么?使用preg_match比使用stripos好?谢谢规则表达式(
preg_match
)更强大、更灵活,能够匹配复杂的文本模式。在这种情况下,
stripos()
充分解决了字母大小写问题,因此您不需要增加复杂性。@RegisteredUser为什么?使用preg_match比使用stripos?感谢正则表达式(
preg_match
)更强大、更灵活,能够匹配复杂的文本模式。在这种情况下,
stripos()
充分解决了字母大小写问题,因此不需要增加复杂性。