Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

smiliar_text() function in PHP

Posted on 2014-01-13
4
Medium Priority
?
375 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 111

Expert Comment

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

Accepted Solution

by:
Ray Paseur earned 2000 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 111

Expert Comment

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

Featured Post

Independent Software Vendors: 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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
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.

719 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