Jump to content

Reverse PREG_MATCH


squiggerz

Recommended Posts

Maybe there's a part of the regular preg_match function that I missed that actually does this, but what I'm trying to accomplish is:

 

Say you have a text file with a sentence or two, here's an example:

 

"Hey, I have a sentence here! Hooray for sentences? No, not really.."

 

Say you input some text into a variable that looks nearly identical to this, but there are small errors, maybe some missing punctuation points. Now I know that preg_match will find matches, but is there any way to find non-matches, so as to throw a flag at the variabled text with preg_match or a similar function? I've been racking my brain for a while and cant seem to find a function that... eh.. matches what I'm looking for. ^o^

 

Thanks in advance for any help.

 

Sq

Link to comment
Share on other sites

Guess I could have elaborated that better after all :D

 

Like this:

<?

$checktext = file_get_contents("sometext.txt");

// the only line in sometext.txt is: Hey, I have a sentence here! Hooray for sentences? No, not really...

/* notice slight differences, 3 periods at the end, only two in the var below, missing exclamation point etc.. 
*/

$text = "Hey, I have asentence here. Hoooray for sentences? No, not really..";

//now some pseudo-code:

check ($text against $checktext)
WHERE $text != $checktext insert into an array

?>

 

Hope that explains a little better of what I'm trying to get at. The more I am thinking about it, the more I am leaning towards having to explode both variables by whitespace, count the whitespaces, and compare arrays, just hoping there's a simpler way around it.

Link to comment
Share on other sites

Nope... that doesn't clarify anything much at all. All I can suggest is you need to look into regular expressions, after all, that is what preg_match is for. from there, you just need to come up with an expression that matches what your looking for. This can include deviations.

Link to comment
Share on other sites

I'm thinking though, maybe it doesn't need to be a preg_match after all..

 

strpos apparently takes full strings in variables and matches them against whatever you give it so maybe if I can create some sort of explode/foreach/strpos loop system, it might work better.

 

The idea is to match specifically to the text file and wherever it does not match, put the unmatched area into a variable to display later. It will work as a sort of pseudo-spell checker, where the only dictionary (so to speak) to match against is the text file. The person would submit a input box via a form, how they'll get the text to type here would be a recording. So more or less its like a mild form of dictation, and the text file will grade the person's accuracy and record the errors exactly.

 

I'll try the strpos thing first.. if that doesnt get me anywhere I'll be sure to move this to the regex area to see if somebody there can come up with something. Did this post clear anything up??? I'm starting to feel a little Punjabi here lol

Link to comment
Share on other sites

Kind of.. like a spelling/grammar checker that checks against a text file. Apparently I'm having a harder time explaining this than I'm probably going to have putting a solution together.

 

Say for instance I have a .txt file on a server. This file can have any characters in any situation possible, but for our purposes it will have a paragraph of random sentences. This file however will serve as the dictionary to which the input is checked by.

 

On the same server will reside a web page with a form that just has a text box. Now, consider that you do not know what is in the text file, but I will read it to you. You have to type out what was said to you into the text box, when you submit, the script will check what you typed against the text file looking for differences. They could be spelling errors, too many whitespaces, combining two words, improper punctuation?!.?!?!. So it's like I'm not looking for what matches in the comparison of the two strings in question, I'm looking for what does NOT match.

 

I'm trying something out with quite a few loops and if's and strtok, strpos, etc right now, I'm just hoping somebody out there knows of a simpler way (maybe a function I haven't found yet that might shortcut this a little bit.

 

Did this make any sense?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.