在以下代码中,是否可以将对象$author
拆分为white space occurs
?
<?php
$url="http://search.twitter.com/search.rss?q=laugh";
$twitter_xml = simplexml_load_file($url);
foreach ($twitter_xml->channel->item as $key) {
$a = $key->{"author"};
echo $a;
}
?>
答案 0 :(得分:2)
$split = explode(' ', (string) $key->{"author"}));
OR
$split = preg_split('/\s+/', (string) $key->{"author"}));
要按@拆分,只需获取$ split并在循环中运行
foreach($split as $key => $value) {
$eta = explode('@', $value);
var_dump($eta);
}
要检查字符串是否存在,请使用strpos
foreach($split as $key => $value) {
if (strpos($value, '@') !== 0) echo 'found';
}
答案 1 :(得分:0)
使用explode:
$array = explode(' ', $key->{"author"});
答案 2 :(得分:0)
有一个爆炸功能可以轻松实现这一目标。所以,例如:
$a = $key->{"author"};
$author = explode(" ", $a);
$first_name = $author[0];
$last_name = $author[1];
希望有所帮助。
答案 3 :(得分:0)
假设你只关心得到两部分:电子邮件和“友好名称”(因为人们有1到n个名字)。
<?php
$url="http://search.twitter.com/search.rss?q=laugh";
$twitter_xml = simplexml_load_file($url);
foreach ($twitter_xml->channel->item as $key) {
$a = $key->{"author"};
preg_match("/([^ ]*) (.*)/", $a, $matches);
print_r($matches);
echo "\n";
}
?>