Php - 通过在字符串中分解特定单词
Php - split specific word by explode in string
我想从字符串中拆分单词。例如,我的字符串是 "In the #name of god" 而我只需要 "name" !!
但是当我使用这个片段时,return 我 "name of god"
$string = "In the #name of god";
$word = explode( '#', $string );
echo $word;
编辑
对不起,我刚才看错了
Explode 将字符串转换为数组。
所以你的输出将导致 ["In the ", "name of god"]。如果你想了解它,你需要更具体地说明它是如何工作的。如果您只想捕捉主题标签后的第一个词,您应该使用 strpos and substr.
$string = "In the #name of god";
$hashtag_pos = strpos($string, "#");
if($hashtag_pos === false)
echo ""; // hashtag not found
else {
$last_whitespace_after_hashtag = strpos($string, " ", $hashtag_pos);
$len = $last_whitespace_after_hashtag === false ? strlen($string)-($hashtag_pos+1) : $last_whitespace_after_hashtag - ($hashtag_pos+1);
echo substr($string, $hashtag_pos+1, strpos($string, " ", $len));
}
$string = "In the #name of god";
// Using `explode`
$word = @reset(explode(' ', end(explode( '#', $string ))));
echo $word; // 'name'
// Using `substr`
$pos1 = strpos($string, '#');
$pos2 = strpos($string, ' ', $pos1) - $pos1;
echo substr($string, $pos1 + 1, $pos2); // 'name'
Note: The @
character before the reset
function is an Error Control Operators. It avoid to display a warning message when using end
function with a non-reference variable, and yes, it's a bad practice. You should create your own variable and pass to end
function. Like this:
// Using `explode`
$segments = explode( '#', $string );
$segments = explode(' ', end($segments));
$word = reset($segments);
echo $word; // 'name'
尝试正则表达式和 preg_match
$string = "In the #name of god";
preg_match('/(?<=#)\w+/', $string, $matches);
print_r($matches);
输出:
Array ( [0] => name )
有几个选项(preg_match 也有助于“#”的多个实例)
<?php
//With Explode only (meh)
$sen = "In the #name of god";
$w = explode(' ', explode('#',$sen)[1])[0];
echo $w;
//With substr and strpos
$s = strpos($sen , '#')+1; // find where # is
$e = strpos(substr($sen, $s), ' ')+1; //find i
$w = substr($sen, $s, $e);
echo $w;
//with substr, strpos and explode
$w = explode(' ', substr($sen, strpos($sen , '#')+1))[0];
echo $w;
在我自己的项目中,我肯定会避免进行一些函数调用来复制一个 preg_
函数调用可以完成的工作。
实际上,匹配文字 #
,然后用 \K
“忘记”它,然后匹配一个或多个非空白字符。如果字符串中有匹配项,则通过索引 0
访问完整字符串匹配项。
代码:(Demo)
$string = "In the #name of god";
echo preg_match('~#\K\S+~', $string, $match) ? $match[0] : '';
// output: name
我想从字符串中拆分单词。例如,我的字符串是 "In the #name of god" 而我只需要 "name" !! 但是当我使用这个片段时,return 我 "name of god"
$string = "In the #name of god";
$word = explode( '#', $string );
echo $word;
编辑
对不起,我刚才看错了
Explode 将字符串转换为数组。 所以你的输出将导致 ["In the ", "name of god"]。如果你想了解它,你需要更具体地说明它是如何工作的。如果您只想捕捉主题标签后的第一个词,您应该使用 strpos and substr.
$string = "In the #name of god";
$hashtag_pos = strpos($string, "#");
if($hashtag_pos === false)
echo ""; // hashtag not found
else {
$last_whitespace_after_hashtag = strpos($string, " ", $hashtag_pos);
$len = $last_whitespace_after_hashtag === false ? strlen($string)-($hashtag_pos+1) : $last_whitespace_after_hashtag - ($hashtag_pos+1);
echo substr($string, $hashtag_pos+1, strpos($string, " ", $len));
}
$string = "In the #name of god";
// Using `explode`
$word = @reset(explode(' ', end(explode( '#', $string ))));
echo $word; // 'name'
// Using `substr`
$pos1 = strpos($string, '#');
$pos2 = strpos($string, ' ', $pos1) - $pos1;
echo substr($string, $pos1 + 1, $pos2); // 'name'
Note: The
@
character before thereset
function is an Error Control Operators. It avoid to display a warning message when usingend
function with a non-reference variable, and yes, it's a bad practice. You should create your own variable and pass toend
function. Like this:
// Using `explode`
$segments = explode( '#', $string );
$segments = explode(' ', end($segments));
$word = reset($segments);
echo $word; // 'name'
尝试正则表达式和 preg_match
$string = "In the #name of god";
preg_match('/(?<=#)\w+/', $string, $matches);
print_r($matches);
输出:
Array ( [0] => name )
有几个选项(preg_match 也有助于“#”的多个实例)
<?php
//With Explode only (meh)
$sen = "In the #name of god";
$w = explode(' ', explode('#',$sen)[1])[0];
echo $w;
//With substr and strpos
$s = strpos($sen , '#')+1; // find where # is
$e = strpos(substr($sen, $s), ' ')+1; //find i
$w = substr($sen, $s, $e);
echo $w;
//with substr, strpos and explode
$w = explode(' ', substr($sen, strpos($sen , '#')+1))[0];
echo $w;
在我自己的项目中,我肯定会避免进行一些函数调用来复制一个 preg_
函数调用可以完成的工作。
实际上,匹配文字 #
,然后用 \K
“忘记”它,然后匹配一个或多个非空白字符。如果字符串中有匹配项,则通过索引 0
访问完整字符串匹配项。
代码:(Demo)
$string = "In the #name of god";
echo preg_match('~#\K\S+~', $string, $match) ? $match[0] : '';
// output: name