Welcome to WebmasterWorld Guest from 34.229.194.198

Forum Moderators: coopster & jatar k & phranque

Finding differences between two strings

     
1:39 am on Jan 3, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month

joined:Mar 15, 2013
posts: 1002
votes: 96


Let's say that I have two strings:

$first = 'this is a stupid example, csdude is a dang fool';
$second = 'this is a example, csdude is a **** fool';

I would like to compare the two, and denote the differences in some way. Ideally, the outcome would be:

$result = 'this is a <strike>stupid</strike> example, csdude is a <strike>dang</strike> fool';

In practice the first string is submitted by the user, then a series of filters are ran to create the second string. There could be any number of words in the second string that are missing or redacted from the first string, but the second string will never contain more than the first string.

I considered changing the filter to change matches to <strike>whatever</strike>, and then after it's over change <strike>whatever</strike>, but it's never consistent... sometimes it would be ****, sometimes it would be $1**** or ****$2, or just removed entirely with no redacting.
1:55 am on Jan 3, 2019 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11566
votes: 182


make arrays of tokens by splitting the strings on blanks.
loop through the original string's array, comparing item-by-item with the next available token in the edited string's array values.
if they match, append the token to the results string. increment both array indices.
if they don't match but the edited string's token is the redaction string, append the strike element containing the original token to the results string. increment both array indices.
if they don't match and the edited string's token is not the redaction string, append the strike element containing the original token to the results string. increment only the original string's array index.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members