Php 我怎样才能得到字符串后面的单词?

Php 我怎样才能得到字符串后面的单词?,php,strpos,Php,Strpos,我有一个字符串,其中某处包含样式名称:Something。我想能够做的是搜索样式名称:并返回某物或任何该值 我知道我需要使用strpos来搜索字符串,但我在获取值时遇到了很大困难。您可以使用preg\u match\u all: $input = "Sample text Style Name: cats and also this Style Name: dogs"; preg_match_all("/\bStyle Name:\s+(\S+)/", $input, $matches); pr

我有一个字符串,其中某处包含
样式名称:Something
。我想能够做的是搜索
样式名称:
并返回
某物
或任何该值


我知道我需要使用
strpos
来搜索字符串,但我在获取值时遇到了很大困难。

您可以使用
preg\u match\u all

$input = "Sample text Style Name: cats and also this Style Name: dogs";
preg_match_all("/\bStyle Name:\s+(\S+)/", $input, $matches);
print_r($matches[1]);
这张照片是:

Array
(
    [0] => cats
    [1] => dogs
)

使用的模式
\b样式名称:\s+(\s+)
匹配
样式名称:
,后跟一个或多个空格。然后,它匹配并捕获下面的下一个单词。

您可以使用
preg\u match\u all

$input = "Sample text Style Name: cats and also this Style Name: dogs";
preg_match_all("/\bStyle Name:\s+(\S+)/", $input, $matches);
print_r($matches[1]);
这张照片是:

Array
(
    [0] => cats
    [1] => dogs
)

使用的模式
\b样式名称:\s+(\s+)
匹配
样式名称:
,后跟一个或多个空格。然后,它匹配并捕获后面的下一个单词。

使用正向查找

<?php
$string="Style Name: Something with colorful";
preg_match('/(?<=Style Name: )\S+/i', $string, $match);
echo $match[0];
?>


演示:

具有积极的后视功能

<?php
$string="Style Name: Something with colorful";
preg_match('/(?<=Style Name: )\S+/i', $string, $match);
echo $match[0];
?>

演示:您不需要正则表达式。
两次简单的分解,您就得到了样式名称

$str = "something something Style Name: Something some more text";

$style_name = explode(" ",explode("Style Name: ", $str)[1])[0];

echo $style_name; // Something
您不需要正则表达式。
两次简单的分解,您就得到了样式名称

$str = "something something Style Name: Something some more text";

$style_name = explode(" ",explode("Style Name: ", $str)[1])[0];

echo $style_name; // Something

另一个选项是使用
\K
忘记匹配的内容,并将水平空白的0+倍匹配到
\h*

\bStyle Name:\h*\K\S+
|

结果

Array
(
    [0] => Something
    [1] => Something
    [2] => Something
)

另一个选项是使用
\K
忘记匹配的内容,并将水平空白的0+倍匹配到
\h*

\bStyle Name:\h*\K\S+
|

结果

Array
(
    [0] => Something
    [1] => Something
    [2] => Something
)

使用带有正则表达式的正则表达式,如果这是实际需求,则在
上拆分可能更容易。如果这是实际需求,则在
上拆分可能更容易。