Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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_Regex_Strpos - Fatal编程技术网

PHP抓取某个单词并忽略字符串中的其他单词

PHP抓取某个单词并忽略字符串中的其他单词,php,regex,strpos,Php,Regex,Strpos,我试图在一个大约30行的句子列表中只抓取用户名。我可以让它抓取用户名和一切后,但我不想得到用户名后的一切 test.txt [India] Hagiwara has been inactive for 33 days (last seen: Sun Jan 25 23:35:35 2015). [India] Psyborg has been inactive for 35 days (last seen: Fri Jan 23 18:43:58 2015). [Echo] Pela has b

我试图在一个大约30行的句子列表中只抓取用户名。我可以让它抓取用户名和一切后,但我不想得到用户名后的一切

test.txt

[India] Hagiwara has been inactive for 33 days (last seen: Sun Jan 25 23:35:35 2015).
[India] Psyborg has been inactive for 35 days (last seen: Fri Jan 23 18:43:58 2015).
[Echo] Pela has been inactive for 31 days (last seen: Tue Jan 27 20:00:30 2015).
PHP


这可能比您预期的要多,但这里有一种避免正则表达式的方法

$string = substr($lines, $pos+2);
$string = substr($string, 0, strpos($string, ' '));

这可能比您预期的要多,但这里有一种避免正则表达式的方法

$string = substr($lines, $pos+2);
$string = substr($string, 0, strpos($string, ' '));

给定示例字符串,并基于用户名永远不包含空格字符的假设:

<?php
$input_line = "[India] Hagiwara has been inactive for 33 days (last seen: Sun Jan 25 23:35:35 2015).";
if (preg_match("/\[.+\]\s*(\S+)\s*.*/", $input_line, $output_array) == 1) {
    echo $output_array[1];
}
?>


将打印“Hagiwara”。

根据您的示例字符串,并基于用户名从不包含空格字符的假设:

<?php
$input_line = "[India] Hagiwara has been inactive for 33 days (last seen: Sun Jan 25 23:35:35 2015).";
if (preg_match("/\[.+\]\s*(\S+)\s*.*/", $input_line, $output_array) == 1) {
    echo $output_array[1];
}
?>


将打印出“Hagiwara”。

执行此操作的正则表达式并不复杂。它更防弹

$str1=“[印度]哈吉瓦拉已经停止活动33天(最后一次露面时间:2015年1月25日星期日23:35:35)。”

$str2=“[印度]Psyborg已停用35天(上次查看时间:2015年1月23日星期五18:43:58)。”

$str3=“[Echo]Pela已停用31天(最后一次查看时间:2015年1月27日星期二20:00:30)。”

$strs=array($str1、$str2、$str3)

foreach($strs as$key=>$val){

//从行的开头去除带括号的初始字符串
$trimmed\u val=preg\u replace(“/^\[\w+\]\s/”,“”,$val);
//从修剪过的字符串中获取第一个单词(本例中为用户)
预匹配(“/^\w+\s/”,$trimmed\u val,$users);
//打印用户列表以供演示
echo$users[0]。

}

执行此操作的正则表达式并不复杂。它更防弹

$str1=“[印度]哈吉瓦拉已经停止活动33天(最后一次露面时间:2015年1月25日星期日23:35:35)。”

$str2=“[印度]Psyborg已停用35天(上次查看时间:2015年1月23日星期五18:43:58)。”

$str3=“[Echo]Pela已停用31天(最后一次查看时间:2015年1月27日星期二20:00:30)。”

$strs=array($str1、$str2、$str3)

foreach($strs as$key=>$val){

//从行的开头去除带括号的初始字符串
$trimmed\u val=preg\u replace(“/^\[\w+\]\s/”,“”,$val);
//从修剪过的字符串中获取第一个单词(本例中为用户)
预匹配(“/^\w+\s/”,$trimmed\u val,$users);
//打印用户列表以供演示
echo$users[0]。
}

<?php
$input_line = "[India] Hagiwara has been inactive for 33 days (last seen: Sun Jan 25 23:35:35 2015).";
if (preg_match("/\[.+\]\s*(\S+)\s*.*/", $input_line, $output_array) == 1) {
    echo $output_array[1];
}
?>
  //strips the initial bracketed string from the beginning of the line
  $trimmed_val = preg_replace("/^\[\w+\]\s/",'',$val);

  //grabs the first word from the trimmed string (user in this case)
  preg_match("/^\w+\s/",$trimmed_val,$users);

  //prints out a list of users for demonstration
  echo $users[0] . '<br/>';