PHP Classes

CompareStr: Compare strings to find which characters changed

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 118 All time: 9,534 This week: 455Up
Version License PHP version Categories
comparestr 1.0GNU General Publi...5Algorithms, PHP 5, Text processing
Description 

Author

This class can compare strings to find which characters changed.

It takes two strings and traverse the list of their characters to find which characters were changed.

The class returns the changed characters and the respective positions.

Picture of Ovunc Tukenmez
  Performance   Level  
Name: Ovunc Tukenmez <contact>
Classes: 14 packages by
Country: Turkey Turkey
Innovation award
Innovation award
Nominee: 6x

Example

<?php
require_once("CompareStr.php");

// strings
$old_string = '285427';
$new_string = '296427';

// initialize CompareStr class
$CompareStr = new CompareStr($old_string, $new_string);

// compare two strings
$result = $CompareStr->compare();

// print the results using the $result array
echo 'Old string: ' . $old_string. '<br>';
echo
'New string: ' . $new_string . '<br>';
echo
'Number of changes: ' . $result['number_of_changes'] . '<br>';
echo
'Commonly used sequenced chars count: ' . $result['common_chars_length'] . '<br>';

// print two strings with the bold commonly used characters to reflect changes visually
echo '<br>';
for (
$i=0; $i<strlen($old_string); $i++)
{
   
$char = $old_string[$i];
    if (
in_array($i, $result['common_char_indexes_old'])){
        echo
'<b>' . $char . '</b>';
    }
    else{
        echo
$char;
    }
}
echo
'<br>';
for (
$i=0; $i<strlen($new_string); $i++)
{
   
$char = $new_string[$i];
    if (
in_array($i, $result['common_char_indexes_new'])){
        echo
'<b>' . $char . '</b>';
    }
    else{
        echo
$char;
    }
}

/*
sample output:

Old string: 285427<br>
New string: 296427<br>
Number of changes: 2<br>
Commonly used sequenced chars count: 4<br>
<br>
<b>2</b>85<b>4</b><b>2</b><b>7</b><br>
<b>2</b>96<b>4</b><b>2</b><b>7</b>

*/


Details

CompareStr PHP Class

<strong>CompareStr</strong> is a <strong>PHP</strong> class that can be used to can find number of changed chars as well as indexes of commonly used sequenced chars between two strings.

Example script is included in the package. (example.php)<br /> <h2>Sample Output <ul> <li> Old string: 285427<br> New string: 296427<br> Number of changes: 2<br> Commonly used sequenced chars count: 4<br> <br> <b>2</b>85<b>4</b><b>2</b><b>7</b><br> <b>2</b>96<b>4</b><b>2</b><b>7</b> </li> </ul>

<h2>Public Methods of Class</h2> <ul> <li>setOldString()</li> <li>getOldString()</li> <li>setNewString()</li> <li>getNewString()</li> <li>compare()</li> </ul>

<h2>Class Requirements</h2> <ul> <li>PHP 5 >= 5.5.0</li> </ul>

Thanks for checking out this class! If you have improvement idea or bug fix, please contribute this class.<br /> <br /> <br /> Ovunc Tukenmez ovunct@live.com


  Files folder image Files (3)  
File Role Description
Plain text file CompareStr.php Class Class source
Accessible without login Plain text file example.php Example Example script
Accessible without login Plain text file README.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:118
This week:0
All time:9,534
This week:455Up