RE: Re[2]: One more time about regexes

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




> -----Original Message-----
> From: Andre Polykanine [mailto:andre@xxxxxxxx]
> Sent: 27 May 2010 09:14
> To: Adam Richardson
> Cc: php-general@xxxxxxxxxxxxx
> Subject: Re[2]:  One more time about regexes
> 
> Hello Adam,
> 
> You did understand me exactly and perfectly).
> Ordering arrays is a good idea but I don't know how to do that
> exactly.
> For instance, there's a cypher called Polybius square. You write in
> the alphabet in a grid like this:
> 1 a b c d e
> 2 f g h i j
> 3 k l m n o
> 4 p q r s t
> 5 u v w x y
> 6 z
> 
> There is a way where i/j are equal to make a 5 by 5 square but for
> me
> it's equal since I'm working with a 33-letter Russian alphabet, so
> no
> way to make it a perfect square.
> Anyway, a letter has two coordinates and is represented by a two-
> digit
> number. For example, E is 15, K is 21, Z is 61 and so on.
> So we have a word, say, PHP. It will look like this: 412341.

Well, as you're wanting to replace pairs of digits, I'd search for exactly that -- but I think I'd use the e flag to employ a programmed replacement, thus:

    $str = "4123415 - 2444 452315 12154445!";
    $replaced = preg_replace("{(\d\d)}e", "polybius_decode('\\1')", $str);

    echo $replaced;

    function polybius_decode($pair) {

       static $polybius = array(
                                  '11'=>'a', 'b', 'c', 'd', 'e',
                                  '21'=>'f', 'g', 'h', 'i', 'j',
                                  '31'=>'k', 'l', 'm', 'n', 'o',
                                  '41'=>'p', 'q', 'r', 's', 't',
                                  '51'=>'u', 'v', 'w', 'x', 'y',
                                  '61'=>'z'
                               );
						
       return isset($polybius[$pair]) ? $polybius[$pair] : $pair;
    }

Seems to work: "php5 - is the best!". :)

Cheers!

Mike
 -- 
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,  
Leeds Metropolitan University, C507, Civic Quarter Campus, 
Woodhouse Lane, LEEDS,  LS1 3HE,  United Kingdom 
Email: m.ford@xxxxxxxxxxxxxx 
Tel: +44 113 812 4730





To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux