Re: [PHP] Regular expressions
- From: ceo@xxxxxxxxx ("Richard Lynch")
- Date: Tue, 21 Nov 2006 13:07:31 -0600 (CST)
On Tue, November 14, 2006 7:57 pm, John Meyer wrote:
Is there a way to make a regular expression to match on a particular
way
the letters are arranged? For instance, if you had a word:
THAT
It could match on any word in the dictionary that had the form:
1231
I suspect if you worked hard enough at it you could get a regex to do
what you want...
But it might be a heck of a lot easier to just build a database like
this:
<?php
$path = "/usr/share/web2"; //Webster's 2nd ed. dictionary, public
domain, on some distros...
$file = fopen($path, 'r') or die("You may need to cp $path to your
own dir, as it is not readable here...");
while (!feof($file)){
$word = fgets($file, 1000000);
$code = wordcode($word);
//store $code | $word in some convenient way...
//perhaps a database
//perhaps each $code could be a file with all the $word[s] in it
//etc
}
function wordcode($word){
$code = array();
$result = '';
$len = strlen($word);
for ($i = 0; $i < $len; $i++){
$c = $word[$i]; //or use substr if you hate string as array syntax
if (!isset($code[$c])){
$code[$c] = count($code) + 1;
}
$d = $code[$c];
$result .= $d;
}
}
?>
NOTE:
For any word with more then 9 unique characters, you are going to have
SERIOUS problems...
Consider this:
supercalifragilisticexpialidocious
123456789105711989...
How can you tell that '11' for the 'g' isn't supposed to be '1'
followed by '1' for two 's' characters in a row?
supercalifrassilisticexpialidocious
123456789105711989...
You may need to represent your digits in, say, hex form, or even in
base 26 form, which gives you the full alphabet as unique digits.
--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?
.
- Follow-Ups:
- Re: [PHP] Regular expressions
- From: Paul Novitski
- Re: [PHP] Regular expressions
- References:
- Regular expressions
- From: John Meyer
- Regular expressions
- Prev by Date: Re: [PHP] 3d modeling-php
- Next by Date: Re: [PHP] __get() accessor question (a bug maybe?)
- Previous by thread: Re: [PHP] Regular expressions
- Next by thread: Re: [PHP] Regular expressions
- Index(es):