View Single Post
ישן 04-12-09, 11:46   # 9
Shay Ben Moshe
משתמש - היכל התהילה
 
מיני פרופיל
תאריך הצטרפות: Oct 2007
הודעות: 1,397

Shay Ben Moshe לא מחובר  

היי ניצן, בניתי לך פונקצייה שמבוססת על הsimilar_text הזה, היא מקבל מערך של stringים ומחזירה לך מערך עם האחוזים:
PHP קוד:
function match_array_strings($array)
{
    if(!
is_array($array)) return array();
    
$count count($array);
    
$match = array();
    for(
$i=0;$i<$count;$i++)
        for(
$j=$i+1;$j<$count;$j++)
            
similar_text($array[$i],$array[$j],$match[$i.'-'.$j]);
    return 
$match;

לדוגמה, עבור:
PHP קוד:
$input[] = 'my name is shay';
$input[] = 'your name is shay';
$input[] = 'your name was shay';
$input[] = 'my name is shai';

print_r(match_array_strings($input)); 
מוחזר:
PHP קוד:
Array
(
    [
0-1] => 87.5
    
[0-2] => 78.787878787879
    
[0-3] => 93.333333333333
    
[1-2] => 91.428571428571
    
[1-3] => 81.25
    
[2-3] => 72.727272727273

בהצלחה!

עריכה: עם שינוי קטן ניתן לעשות שזה לא יהיה רק 0-1 אלא גם 1-0 וכדומה, מה שמקל על הפנייה למערך אבל יכביד על הזיכרון אם יהיו הרבה טקסטים. הפונקצייה שעושה זאת:
PHP קוד:
function match_array_strings($array)
{
    if(!
is_array($array)) return array();
    
$count count($array);
    
$match = array();
    for(
$i=0;$i<$count;$i++)
        for(
$j=$i+1;$j<$count;$j++)
        {
            
similar_text($array[$i],$array[$j],$match[$i.'-'.$j]);
            
$match[$j.'-'.$i] = $match[$i.'-'.$j];
        }
    return 
$match;

__________________
שי בן משה - בונה אתרים
חותך אתרים, ומתכנת צד לקוח וצד שרת.

Last edited by Shay Ben Moshe; 04-12-09 at 11:50..
  Reply With Quote