Solved

smiliar_text() function in PHP

Posted on 2014-01-13
4
364 Views
Last Modified: 2014-01-13
I have a question regarding similar_text() function in php.

Is there a way so that if there are two strings with the same words but in different order to return 100%?

For example:
similar_text("George Michael", "Michael George", $percent1); //Returns 50
similar_text("George Michael", "George Michael", $percent2); //Returns 100

Open in new window


Thanks a lot!
John
0
Comment
Question by:infodigger
  • 3
4 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39776222
Sure. Make an array from the words and sort the array.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39776246
Here's an example.  There may be more-or-less normalization on the input strings, depending on whether you want to consider punctuation, etc.

http://www.laprbass.com/RAY_temp_infodigger.php

<?php // RAY_temp_infodigger.php
error_reporting(E_ALL);
echo '<pre>';


// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28336893.html


$str = 'Oh say can you see by the dawns early light';
$alt = 'Oh you can the see by say dawns light early';

// BREAK STRINGS APART TO MAKE ARRAYS
$str_arr = explode(' ', $str);
$alt_arr = explode(' ', $alt);

// SORT THE ARRAYS
sort($str_arr);
sort($alt_arr);

// RECOVER THE STRINGS
$str_new = implode(' ', $str_arr);
$alt_new = implode(' ', $alt_arr);

// SHOW THE ORIGINAL
echo PHP_EOL . $str;
echo PHP_EOL . $alt;
echo PHP_EOL;
similar_text($str, $alt, $pct);
echo $pct;

// SHOW THE SORTED RESULTS
echo PHP_EOL . $str_new;
echo PHP_EOL . $alt_new;
echo PHP_EOL;
similar_text($str_new, $alt_new, $pct_new);
echo $pct_new;

Open in new window

0
 

Author Closing Comment

by:infodigger
ID: 39776249
Thanks Ray!

As always a very complete and useful answer:)

Kind regards,
John
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39776315
Thanks, John.  Glad to help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now