Solved

smiliar_text() function in PHP

Posted on 2014-01-13
4
371 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 110

Expert Comment

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

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 110

Expert Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

738 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