我需要从两个字符串中提取常用文本。
示例:
$text1 = "My name is John. I like apples. I drive a car. Nice to meet you";
$text2 = "My name is John. I like pears. I don't drive.";
#TODO function
$text3 = get_common_text($text1,$text2);
echo $text3;
//Result: "My name is John. I like . I drive."
答案 0 :(得分:1)
试试这个,我认为这是对的
$text1 = explode( ' ', "My name is John. I like apples. I drive a car. Nice to meet you" );
$text2 = explode( ' ', "My name is John. I like pears. I don't drive." );
foreach( $text2 as $key )
{
if( strpos( $key, '.' ) !== false )
{
$temp = explode( '.', $key );
$text2[$key] = $temp[0];
}
}
$common = array();
foreach( $text1 as $key )
{
if( strpos( '.', $key ) )
{
$temp = explode( $key, '.' );
echo $temp[0];
}
if( in_array( $key, $text2 ) )
{
$common[] = $key;
}
}
$common = implode( ' ', $common );
echo $common;
输出为My name is John. I like I drive