2012-07-01 69 views
1

使用PHP的sscanf读取用户提交的句子并尝试从中获取数据是否合适?例如:使用sscanf传递'模板语句'?

用户输入:

"James is child of Bill",我想使$child="james"$parent="bill"

或:"Mary is the mother of kate",我想使$child="kate"$parent="mary"

可能有相当多的其他可能句子格式,我将不得不提取数据(不超过100个小时)。

所以我应该使用:

$templates = array("%s is a child of %s","%s is the mother of %s"); 
$i=0; 
$max = count($templates); 
while(($test == -1) && (i < $max)) { 
    $test = sscanf($userInput, $templates[i]); 
    $i++; 
} 
if(i < $max) { 
    sscanf($userInput, $templates[i],$child,&parent);//hmm, what if $child,&parent should be the other way around? 
} else { 
    echo "template not found for sentence"; 
} 

?所以,我可以看到,有人会提出一个更好的方法,或者让这个解决方案起作用的方法吗?

真的很感激!

回答