The following code takes in an $input like: "a banana is a fruit"
$t = sscanf($input,"a %s is a %s",$child,$parent);
echo $t.",".$child.",".$parent;
and it outputs: 2,banana,fruit, which is perfect!
However, if $input = "a cow is an animal" we get the output: 2,cow,n. This is not ideal.
Ideally, for $input = "a cow is an animal" it would return 1,cow, and I will use a separate formatting string: "a %s is an %s" for this input which would return 2,cow,animal.
So: why doesn’t the space after the second a in "a %s is a %s" stop this problem from occurring? Is there a way to explicitly declare that a space MUST occur after that a for it to find a correct match?
Hopefully that makes sense. Any help appreciated!
EDIT: here is the actual code:
$teachArray = array("a %s is a %s","an %s is a %s","a %s is an %s","an %s is an %s");
$i=0;
$t = -1;
while( (t <= 2) && (i < count($teachArray)) ) {
$t = sscanf($input,$teachArray[i],$child,$parent);
$i++;
}
So it keeps looping until it either finds a matching sentence structure, or it runs out of sentence templates (stored in $teachArray).
Instead of scanf, you should use a regular expression: