Solved

smiliar_text() function in PHP

Posted on 2014-01-13
4
368 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 109

Expert Comment

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

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 109

Expert Comment

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

840 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