Re[2]: [PHP] 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. What does preg_replace do? Exactly, it's searching 41, then 12, then 23, and so on. I tried to make a loop: $length=mb_strlen($str); for ($i=0; $i$length; $i+=2) { $pair=mb_substr($str, $i, 2); $pair=preg_replace($numbers, $letters, $str); } It already smells something not good, but anyway. If I do that, I have one more problem: say, we have a phrase PHP is the best. So we crypt it and get: 412341 2444 452315 12154445 Then the parser begins: 41=p, 23=h, 41=p, (attention!) \s2= I marked a whitespace as \s so you see what happens. I might split the string by spaces but I can't imagine what a user inputs: it might be a dash, for example... Sorry for such a long message but I'm really annoyed with this preg_replace's behavior. And it's not the only one task to accomplish... Thanks a lot! -- With best regards from Ukraine, Andre Skype: Francophile; WlmMSN: arthaelon @ yandex.ru; Jabber: arthaelon @ jabber.org Yahoo! messenger: andre.polykanine; ICQ: 191749952 Twitter: m_elensule - Original message - From: Adam Richardson simples...@gmail.com To: php-general@lists.php.net php-general@lists.php.net Date: Thursday, May 27, 2010, 7:56:28 AM Subject: [PHP] One more time about regexes On Wed, May 26, 2010 at 11:10 PM, Nilesh Govindarajan li...@itech7.comwrote: -- Forwarded message -- From: Nilesh Govindarajan li...@itech7.com Date: Thu, May 27, 2010 at 8:40 AM Subject: Re: [PHP] One more time about regexes To: Andre Polykanine an...@oire.org On Thu, May 27, 2010 at 3:33 AM, Andre Polykanine an...@oire.org wrote: Hello everyone, Sorry, but I'm asking one more time (since it's really annoying me and I need to apply some really dirty hacks): Is there a way making preg_replace() pass through the regex one single time searching from left to right and not to erase what it has already done? I can give you a real task I'm accomplishing but the tasks requiring that tend to multiply... Thanks a lot! -- With best regards from Ukraine, Andre Http://oire.org/ - The Fantasy blogs of Oire Skype: Francophile; WlmMSN: arthaelon @ yandex.ru; Jabber: arthaelon @ jabber.org Yahoo! messenger: andre.polykanine; ICQ: 191749952 Twitter: http://twitter.com/m_elensule -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php If you don't want to replace the stuff it has searched then use preg_match ! -- Nilesh Govindarajan Facebook: nilesh.gr Twitter: nileshgr Website: www.itech7.com -- Nilesh Govindarajan Facebook: nilesh.gr Twitter: nileshgr Website: www.itech7.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php If I'm understanding correctly, Andre, you want to perform a replace operation, but you're observing that there's an element of recursion happening (e.g., you replace one item with its replacement, and then the new replacement is also replaced.) To my knowledge, this won't happen with a standard preg_replace(): $string = 'Bil Tom Phil'; $pattern = '//'; $replacement = 'amp;'; // outputs Bil amp; Tom amp; Phil echo preg_replace($pattern, $replacement, $string); However, if you're using arrays to pass in the patterns and replacements, then one array item could later be replaced by another (see comment and example on this page by info at gratisrijden dot nl): http://php.net/manual/en/function.preg-replace.php If that's the case, you have to carefully structure the order of the array items so-as to preclude one replacement having the opportunity to override a subsequent replacement. Sorry if I misunderstood. You might want to create a simple example of code showing what you're working through. Adam -- Nephtali: PHP web framework that functions beautifully http://nephtaliproject.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: Re[2]: [PHP] One more time about regexes
-Original Message- From: Andre Polykanine [mailto:an...@oire.org] Sent: 27 May 2010 09:14 To: Adam Richardson Cc: php-general@lists.php.net Subject: Re[2]: [PHP] 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.f...@leedsmet.ac.uk 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
Re: Re[2]: [PHP] One more time about regexes
On Thu, May 27, 2010 at 4:13 AM, Andre Polykanine an...@oire.org wrote: 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. What does preg_replace do? Exactly, it's searching 41, then 12, then 23, and so on. I tried to make a loop: $length=mb_strlen($str); for ($i=0; $i$length; $i+=2) { $pair=mb_substr($str, $i, 2); $pair=preg_replace($numbers, $letters, $str); } It already smells something not good, but anyway. If I do that, I have one more problem: say, we have a phrase PHP is the best. So we crypt it and get: 412341 2444 452315 12154445 Then the parser begins: 41=p, 23=h, 41=p, (attention!) \s2= I marked a whitespace as \s so you see what happens. I might split the string by spaces but I can't imagine what a user inputs: it might be a dash, for example... Sorry for such a long message but I'm really annoyed with this preg_replace's behavior. And it's not the only one task to accomplish... Thanks a lot! -- With best regards from Ukraine, Andre Skype: Francophile; WlmMSN: arthaelon @ yandex.ru; Jabber: arthaelon @ jabber.org Yahoo! messenger: andre.polykanine; ICQ: 191749952 Twitter: m_elensule - Original message - From: Adam Richardson simples...@gmail.com To: php-general@lists.php.net php-general@lists.php.net Date: Thursday, May 27, 2010, 7:56:28 AM Subject: [PHP] One more time about regexes On Wed, May 26, 2010 at 11:10 PM, Nilesh Govindarajan li...@itech7.com wrote: -- Forwarded message -- From: Nilesh Govindarajan li...@itech7.com Date: Thu, May 27, 2010 at 8:40 AM Subject: Re: [PHP] One more time about regexes To: Andre Polykanine an...@oire.org On Thu, May 27, 2010 at 3:33 AM, Andre Polykanine an...@oire.org wrote: Hello everyone, Sorry, but I'm asking one more time (since it's really annoying me and I need to apply some really dirty hacks): Is there a way making preg_replace() pass through the regex one single time searching from left to right and not to erase what it has already done? I can give you a real task I'm accomplishing but the tasks requiring that tend to multiply... Thanks a lot! -- With best regards from Ukraine, Andre Http://oire.org/ - The Fantasy blogs of Oire Skype: Francophile; WlmMSN: arthaelon @ yandex.ru; Jabber: arthaelon @ jabber.org Yahoo! messenger: andre.polykanine; ICQ: 191749952 Twitter: http://twitter.com/m_elensule -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php If you don't want to replace the stuff it has searched then use preg_match ! -- Nilesh Govindarajan Facebook: nilesh.gr Twitter: nileshgr Website: www.itech7.com -- Nilesh Govindarajan Facebook: nilesh.gr Twitter: nileshgr Website: www.itech7.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php If I'm understanding correctly, Andre, you want to perform a replace operation, but you're observing that there's an element of recursion happening (e.g., you replace one item with its replacement, and then the new replacement is also replaced.) To my knowledge, this won't happen with a standard preg_replace(): $string = 'Bil Tom Phil'; $pattern = '//'; $replacement = 'amp;'; // outputs Bil amp; Tom amp; Phil echo preg_replace($pattern, $replacement, $string); However, if you're using arrays to pass in the patterns and replacements, then one array item could later be replaced by another (see comment and example on this page by info at gratisrijden dot nl): http://php.net/manual/en/function.preg-replace.php If that's the case, you have to carefully structure the order of the array items so-as to preclude one replacement having the opportunity to override a subsequent replacement. Sorry if I misunderstood. You might want to create a simple example of code showing what you're working through. Adam -- Nephtali: PHP web framework that functions beautifully http://nephtaliproject.com Andre, I'd try something like this: ?php /** * Description of PolybiusSquare * * @author Adam Richardson, creator of the Nephtali Web Framework */ class PolybiusSquare { public $square_values; /** * Creates PolybiusSquare instance for encoding and decoding
[PHP] One more time about regexes
Hello everyone, Sorry, but I'm asking one more time (since it's really annoying me and I need to apply some really dirty hacks): Is there a way making preg_replace() pass through the regex one single time searching from left to right and not to erase what it has already done? I can give you a real task I'm accomplishing but the tasks requiring that tend to multiply... Thanks a lot! -- With best regards from Ukraine, Andre Http://oire.org/ - The Fantasy blogs of Oire Skype: Francophile; WlmMSN: arthaelon @ yandex.ru; Jabber: arthaelon @ jabber.org Yahoo! messenger: andre.polykanine; ICQ: 191749952 Twitter: http://twitter.com/m_elensule -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Fwd: [PHP] One more time about regexes
-- Forwarded message -- From: Nilesh Govindarajan li...@itech7.com Date: Thu, May 27, 2010 at 8:40 AM Subject: Re: [PHP] One more time about regexes To: Andre Polykanine an...@oire.org On Thu, May 27, 2010 at 3:33 AM, Andre Polykanine an...@oire.org wrote: Hello everyone, Sorry, but I'm asking one more time (since it's really annoying me and I need to apply some really dirty hacks): Is there a way making preg_replace() pass through the regex one single time searching from left to right and not to erase what it has already done? I can give you a real task I'm accomplishing but the tasks requiring that tend to multiply... Thanks a lot! -- With best regards from Ukraine, Andre Http://oire.org/ - The Fantasy blogs of Oire Skype: Francophile; WlmMSN: arthaelon @ yandex.ru; Jabber: arthaelon @ jabber.org Yahoo! messenger: andre.polykanine; ICQ: 191749952 Twitter: http://twitter.com/m_elensule -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php If you don't want to replace the stuff it has searched then use preg_match ! -- Nilesh Govindarajan Facebook: nilesh.gr Twitter: nileshgr Website: www.itech7.com -- Nilesh Govindarajan Facebook: nilesh.gr Twitter: nileshgr Website: www.itech7.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] One more time about regexes
On Wed, May 26, 2010 at 11:10 PM, Nilesh Govindarajan li...@itech7.comwrote: -- Forwarded message -- From: Nilesh Govindarajan li...@itech7.com Date: Thu, May 27, 2010 at 8:40 AM Subject: Re: [PHP] One more time about regexes To: Andre Polykanine an...@oire.org On Thu, May 27, 2010 at 3:33 AM, Andre Polykanine an...@oire.org wrote: Hello everyone, Sorry, but I'm asking one more time (since it's really annoying me and I need to apply some really dirty hacks): Is there a way making preg_replace() pass through the regex one single time searching from left to right and not to erase what it has already done? I can give you a real task I'm accomplishing but the tasks requiring that tend to multiply... Thanks a lot! -- With best regards from Ukraine, Andre Http://oire.org/ - The Fantasy blogs of Oire Skype: Francophile; WlmMSN: arthaelon @ yandex.ru; Jabber: arthaelon @ jabber.org Yahoo! messenger: andre.polykanine; ICQ: 191749952 Twitter: http://twitter.com/m_elensule -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php If you don't want to replace the stuff it has searched then use preg_match ! -- Nilesh Govindarajan Facebook: nilesh.gr Twitter: nileshgr Website: www.itech7.com -- Nilesh Govindarajan Facebook: nilesh.gr Twitter: nileshgr Website: www.itech7.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php If I'm understanding correctly, Andre, you want to perform a replace operation, but you're observing that there's an element of recursion happening (e.g., you replace one item with its replacement, and then the new replacement is also replaced.) To my knowledge, this won't happen with a standard preg_replace(): $string = 'Bil Tom Phil'; $pattern = '//'; $replacement = 'amp;'; // outputs Bil amp; Tom amp; Phil echo preg_replace($pattern, $replacement, $string); However, if you're using arrays to pass in the patterns and replacements, then one array item could later be replaced by another (see comment and example on this page by info at gratisrijden dot nl): http://php.net/manual/en/function.preg-replace.php If that's the case, you have to carefully structure the order of the array items so-as to preclude one replacement having the opportunity to override a subsequent replacement. Sorry if I misunderstood. You might want to create a simple example of code showing what you're working through. Adam -- Nephtali: PHP web framework that functions beautifully http://nephtaliproject.com