我正在尝试使用简单的HTML DOM拆分我从网页中提取的名称,并且列表和爆炸功能不能解决问题。我想做的就是取一个名字{firstname middle(optional)lastname}并拆分它们。中间名只显示在一些名字上,如果我能处理它,那将是一个奖励。
以下是代码:
<?php
$data = new simple_html_dom();
$data->load_file("http://www.ratemyprofessors.com/ShowRatings.jsp?tid=861228");
$profName= $data->find("//*[@id=profName]", 0);
$profName = strip_tags($profName);
echo "Full Name: " . $profName = trim($profName);
list($first, $last) = explode(' ', "$profName ");
echo "first name: " . $first;
echo "last name: " . $last;
?>
我的输出显示:
Full Name: Jennifer Aaker
firstname: Jennifer Aaker
lastname:
答案 0 :(得分:3)
尝试:
list($first, $last) = explode(" ", $profName);
答案 1 :(得分:0)
这是一个简单的功能,可以解决这个问题。
function first_last($s) {
/* assume first name is followed by a whitespace character. take everything after for last. middle initial will be returned as part of last. */
$pos = strpos($s,' ');
if ($pos == FALSE) { // if space is not found... call if first name
return array($s,'');
}
$first = substr($s, 0 , $pos);
$last = substr($s,$pos + 1);
return array($first,$last);
}
// test
$s2 = 'john stewart';
list($first,$last) = first_last($s2);