Solved

smiliar_text() function in PHP

Posted on 2014-01-13
4
360 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
Comment Utility
Sure. Make an array from the words and sort the array.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
Thanks Ray!

As always a very complete and useful answer:)

Kind regards,
John
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks, John.  Glad to help!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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 …
The viewer will learn how to count occurrences of each item in an array.
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.

772 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

13 Experts available now in Live!

Get 1:1 Help Now