php 如何获取字符串中某个单词前后的所有内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15605833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get everything before and after a certain word in string
提问by Thomas Lai
how do I get everything in a php string before and after a certain word? For example, if I have a string that says "5 times 8", how do I extract the 5 and the 8 and store them into separate variables? I'm kinda new to PHP.
如何在某个单词之前和之后获取 php 字符串中的所有内容?例如,如果我有一个字符串表示“5 乘以 8”,我如何提取 5 和 8 并将它们存储到单独的变量中?我对 PHP 有点陌生。
I've tried
我试过了
$foo = "5 times 8";
$foo = str_replace(" times ","",$foo);
Then what? The result is "58"
然后呢?结果是“58”
回答by Cal
If you are certain that your input string contains " times "
then you can do this:
如果您确定您的输入字符串包含," times "
那么您可以执行以下操作:
$input = "5 times 8";
list($a, $b) = explode(' times ', $input);
echo $a; // "5"
echo $b; // "8"
If you need to do something a little more complex, use regular expressions:
如果您需要做一些更复杂的事情,请使用正则表达式:
$input = "5 times 8";
if (preg_match('!(\d+)\s*times\s*(\d+)!i', $input, $m)){
$a = $m[1];
$b = $m[2];
}
回答by Nelson
回答by Neeraj
回答by Sebastian Neira
I'm not fully sure of what you're trying, but maybe this can help:
我不完全确定你在尝试什么,但也许这会有所帮助:
If you explode the string with explode($delimiter, $string, [$limit]), being in the case you had as an example "times" as the $delimiter, you can get an array with 5 and 8 as values.
如果您使用explode($delimiter, $string, [$limit]) 分解字符串,例如您将“times”作为$delimiter 的示例,则您可以获得一个值为5 和8 的数组。
Therefore:
所以:
$string = "5 times 8"
$delimiter = " times "
$array = explode($delimiter, $string)
would yield
会屈服
$array[0] = 5
$array[1] = 8
Hope this helps!
希望这可以帮助!
回答by Sepster
You've said in your question "how to get everythingbefore and after a certain word". So this will get everything. Replace the instances of .*
with eg \d*
to get eg numberic digits either side.
你在你的问题中说过“如何在某个词之前和之后得到所有东西”。所以这将得到一切。更换的情况下,.*
与如\d*
获得如数字小数字两侧。
The following will return 0 if no match found at all, or >0 if matches found.
如果根本找不到匹配项,则以下将返回 0,如果找到匹配项,则返回 >0。
preg_match_all('/^(.*)times(.*)$/', $yourStringToSearch, $arr, PREG_PATTERN_ORDER);
preg_match_all('/^(.*)times(.*)$/', $yourStringToSearch, $arr, PREG_PATTERN_ORDER);
If matches found, $arr
will be populated.
如果找到匹配项,$arr
将填充。
So eg if "times" is found, but without pre/post text, then it'll return 1, and $arr[0]
will contain only "times".
因此,例如,如果找到“时间”,但没有前/后文本,则它将返回 1,并且$arr[0]
仅包含“时间”。
So for example, if $yourStringToSearch
contains "5 times 8", then $arr
will be populated as follows (where $arr[0]
is the entire match, $arr[1]
is the pre-text, and $arr[2]
is the post-text).
因此,例如,如果$yourStringToSearch
包含“5 次 8”,则将$arr
按如下方式填充(其中$arr[0]
是整个匹配,$arr[1]
是前文本,$arr[2]
是后文本)。
Array
(
[0] => Array
(
[0] => 5 times 8
)
[1] => Array
(
[0] => 5
)
[2] => Array
(
[0] => 8
)
)
But, be careful because if $yourStringToSearch = "5 _times_ -times- 8"
it'll return:
但是,要小心,因为如果$yourStringToSearch = "5 _times_ -times- 8"
它会返回:
Array
(
[0] => Array
(
[0] => 5 _times_ -times- 8
)
[1] => Array
(
[0] => 5 _times_ -
)
[2] => Array
(
[0] => - 8
)
)
That is - you need to either specify what you want to happen in the case where your search string appears more than once in the target, or accept the "default" in this case where the first match on your search term is "the" match.
也就是说 - 您需要指定在搜索字符串在目标中出现多次的情况下要发生的事情,或者在搜索词的第一个匹配项是“the”匹配项的情况下接受“默认” .
If you need to ensure the search term is surrounded by spaces, then the following two will work. THe first assumes you don't want to "capture" the space, while the second will include the space in the captures:
如果您需要确保搜索词被空格包围,那么以下两个将起作用。第一个假设您不想“捕获”空间,而第二个将在捕获中包含空间:
preg_match_all('/^(.*) times (.*)$/', $yourStringToSearch, $arr, PREG_PATTERN_ORDER);
preg_match_all('/^(.*) times (.*)$/', $yourStringToSearch, $arr, PREG_PATTERN_ORDER);
preg_match_all('/^(.* )times( .*)$/', $yourStringToSearch, $arr, PREG_PATTERN_ORDER);
preg_match_all('/^(.* )times( .*)$/', $yourStringToSearch, $arr, PREG_PATTERN_ORDER);
回答by Olivier Picault
You can use the substr
function : http://www.php.net/manual/en/function.substr.phpand if you don't know the position of the word, use strpos
to get it : http://php.net/manual/en/function.strpos.php
您可以使用该substr
功能:http: //www.php.net/manual/en/function.substr.php,如果您不知道单词的位置,请使用strpos
它:http: //php.net /manual/en/function.strpos.php