Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.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/0/xml/14.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从搜索API中提取Twitter用户名_Php_Xml_Api_Twitter_Atom Feed - Fatal编程技术网

如何通过PHP从搜索API中提取Twitter用户名

如何通过PHP从搜索API中提取Twitter用户名,php,xml,api,twitter,atom-feed,Php,Xml,Api,Twitter,Atom Feed,Twitter搜索api返回ATOM/XML格式的结果,如下所示: <author> <name>username (Friendly Name)</name> <uri>http://twitter.com/username</uri> </author> $names = "username (Friendly Name)" $username = "username" $friendlyName = "Fr

Twitter搜索api返回ATOM/XML格式的结果,如下所示:

<author>
  <name>username (Friendly Name)</name>
  <uri>http://twitter.com/username</uri>
</author>
$names = "username (Friendly Name)"
$username = "username"
$friendlyName = "Friendly Name" (without parantheses)
但我想使用PHP将它们提取为单独的变量,如下所示:

<author>
  <name>username (Friendly Name)</name>
  <uri>http://twitter.com/username</uri>
</author>
$names = "username (Friendly Name)"
$username = "username"
$friendlyName = "Friendly Name" (without parantheses)

蒂亚

我想这样做应该可以:

$names = "username (Friendly Name)";
if (preg_match('/^(.*?) \((.*)\)$/', $names, $m)) {
    var_dump($m[1], $m[2]);
}
在这种情况下,你会得到:

string 'username' (length=8)
string 'Friendly Name' (length=13)
(请注意“
字符串”
”、“
长度”
”,以及所有这些仅是
var_dump
的输出,而不是实际变量内容)


基本上,正则表达式是匹配的:

  • 从字符串开头到空格的任何内容
  • 然后,在
    ()
  • 这是两个返回的模式——它们位于非转义
    ()
    之间;这意味着它们将在
    $m[1]
    $m[2]

    • 我想,像这样的事情应该可以:

      $names = "username (Friendly Name)";
      if (preg_match('/^(.*?) \((.*)\)$/', $names, $m)) {
          var_dump($m[1], $m[2]);
      }
      
      在这种情况下,你会得到:

      string 'username' (length=8)
      string 'Friendly Name' (length=13)
      
      (请注意“
      字符串”
      ”、“
      长度”
      ”,以及所有这些仅是
      var_dump
      的输出,而不是实际变量内容)


      基本上,正则表达式是匹配的:

      • 从字符串开头到空格的任何内容
      • 然后,在
        ()
      • 这是两个返回的模式——它们位于非转义
        ()
        之间;这意味着它们将在
        $m[1]
        $m[2]
        • 非正则表达式解决方案:

          $names = 'username (Friendly Name)';
          list($username, $friendlyName) = explode('(', $names);
          $username = trim($username);
          $friendlyName = trim($friendlyName, ')');
          
          假定括号在两个名称中都无效。

          非正则表达式解决方案:

          $names = 'username (Friendly Name)';
          list($username, $friendlyName) = explode('(', $names);
          $username = trim($username);
          $friendlyName = trim($friendlyName, ')');
          
          假定括号在两个名称中都无效