Text comparison PHP code needed

4 replies
I need to add a routine to an existing php script that will take two strings and identify groups of three or more words in the second that are the same as in the first, but ignoring any punctuation.

So, for example, the two input strings might be:

1. Here is a string of text that is being examined.

2. This string of text is being compared to the other.

And the output would be 'string of text'.

Also, with these two strings:

1. Let's look at things a different way (and add some punctuation); then compare.

2. Now we can look at things differently, by adding some punctuation - then comparing.

The output would be 'look at things' and 'some punctuation - then'.

So the punctuation is effectively ignored in both strings, but preserved in the output from the second string.

I hope all that makes sense!

Martin
#code #comparison #needed #php #text
  • Profile picture of the author Brandon Tanner
    Martin,

    I think regex would be your best bet for that (preg_match). You could do it without regex, but it would be a lot more work, since PHP doesn't have any built-in string functions that will match strings based on your specific requirements (none that I'm aware of, at least).

    I rarely ever use regex myself so I'm afraid I couldn't help you with this specific task, but you might find someone on Fiverr that could knock it out for you. Failing that, try Elance, Guru.com, etc.
    Signature

    {{ DiscussionBoard.errors[8286240].message }}
  • Profile picture of the author Valdor Kiebach
    Have a look here:
    PHP Help!

    This code I wrote works using the linked to method and you could use this as a start and add some other functions to limit it to blocks of 3 words and keep the punctuation of the second string.

    <?php
    $txta="this is the end, beautiful friend"; // string1
    $stra=(str_word_count($txta,1)); // convert string1 to an array
    $txtb="tis the end, my friend"; // string2
    $strb=(str_word_count($txtb,1)); // convert string2 to an array
    print_r($stra);// show the array for string1
    echo('<br />');
    print_r($strb);// show the array for string2
    echo('<br />');
    $result=array_intersect($stra,$strb); // this compares the 2 arrays and stores the result in another array
    print_r($result); // show the resulting array of same words
    echo('<br />');
    $same_words = implode(" ", $result); // convert the resulting array to a string
    echo $same_words; // show the result as a string
    ?>
    {{ DiscussionBoard.errors[8286890].message }}
  • Profile picture of the author octalsoftware
    int strcmp ( string str1, string str2) int strcasecmp ( string str1, string str2)


    Strcmp()
    , and its case-insensitive sibling strcasecmp(), is a quick way of comparing two words and telling whether they are equal, or whether one comes before the other.
    {{ DiscussionBoard.errors[8293804].message }}
  • Profile picture of the author sktthemes
    Hello,
    You can do this by strcmp()

    <?php
    $campo = $_GET['campo'];
    $valor = $_GET['valor'];
    $hello;
    if ($campo == "myPassword")
    {
    if (!preg_match("/^\S{4,12}$/", $valor))
    {
    echo "Tamanho entre 4 e 12 letras e sem espaços";
    }
    else
    {
    $hello = $valor; //problem here echo $hello;
    }
    if ($campo == "passwordMatch")
    {
    if ($hello != $valor ) { echo "Passwords don't match"; }
    }
    ?>
    {{ DiscussionBoard.errors[8299047].message }}

Trending Topics