Re[6]: [PHP] preg_replace: avoiding double replacements
Ash, Actually it's not the Caesar cypher itself (see http://en.wikipedia.org/wiki/Temurah_(Kabbalah), third method), but your way of transformation seems to me the best for a while) Thanks! -- 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: Ashley Sheridan a...@ashleysheridan.co.uk To: Peter Lind peter.e.l...@gmail.com Date: Tuesday, May 18, 2010, 2:32:11 PM Subject: [PHP] preg_replace: avoiding double replacements On Tue, 2010-05-18 at 13:09 +0200, Peter Lind wrote: On 18 May 2010 12:35, Andre Polykanine an...@oire.org wrote: Hello Peter, Hm... I see I need to specify what I'm really doing. Actually, I need to change the letters in the text. It's a famous and ancient crypting method: you divide the alphabet making two parts, then you change the letters of one part with letters for other part (so A becomes N, B becomes O, etc., and vice versa). it works fine and slightly with strtr or str_replace... but only if the text is not in utf-8 and it doesn't contain any non-English letters such as Cyrillic what I need. What my regex does is the following: it sees an A, well it changes it to N; then it goes through the string and sees an N... what does it do? Surely, it changes it back to A! I hoped (in vain) that there exists a modifier preventing this behavior... but it seems that it's false( Thanks! Hmmm, what comes to mind is using your string as an array and translating one character after another, building your output string using a lookup table. Not entirely sure how that will play with utf8 characters, you'd have to try and see. I don't think you'll get any of PHPs string functions to do the work for you - they'll do the job in serial, not parallel. Regards Peter -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 /hype If you're wanting to use the Caesar cypher (for that's what it is) then why not just modify the entire string, character by character, to use a character code n characters ahead. For example, a capital A is ascii 65, you want to change it to an N to add 14 to that. Just keep n the same throughout and it's easy to convert back. Thanks, Ash http://www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re[6]: [PHP] preg_replace: avoiding double replacements
On Wed, 2010-05-19 at 16:40 +0300, Andre Polykanine wrote: Ash, Actually it's not the Caesar cypher itself (see http://en.wikipedia.org/wiki/Temurah_(Kabbalah), third method), but your way of transformation seems to me the best for a while) Thanks! -- 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: Ashley Sheridan a...@ashleysheridan.co.uk To: Peter Lind peter.e.l...@gmail.com Date: Tuesday, May 18, 2010, 2:32:11 PM Subject: [PHP] preg_replace: avoiding double replacements On Tue, 2010-05-18 at 13:09 +0200, Peter Lind wrote: On 18 May 2010 12:35, Andre Polykanine an...@oire.org wrote: Hello Peter, Hm... I see I need to specify what I'm really doing. Actually, I need to change the letters in the text. It's a famous and ancient crypting method: you divide the alphabet making two parts, then you change the letters of one part with letters for other part (so A becomes N, B becomes O, etc., and vice versa). it works fine and slightly with strtr or str_replace... but only if the text is not in utf-8 and it doesn't contain any non-English letters such as Cyrillic what I need. What my regex does is the following: it sees an A, well it changes it to N; then it goes through the string and sees an N... what does it do? Surely, it changes it back to A! I hoped (in vain) that there exists a modifier preventing this behavior... but it seems that it's false( Thanks! Hmmm, what comes to mind is using your string as an array and translating one character after another, building your output string using a lookup table. Not entirely sure how that will play with utf8 characters, you'd have to try and see. I don't think you'll get any of PHPs string functions to do the work for you - they'll do the job in serial, not parallel. Regards Peter -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 /hype If you're wanting to use the Caesar cypher (for that's what it is) then why not just modify the entire string, character by character, to use a character code n characters ahead. For example, a capital A is ascii 65, you want to change it to an N to add 14 to that. Just keep n the same throughout and it's easy to convert back. Thanks, Ash http://www.ashleysheridan.co.uk http://en.wikipedia.org/wiki/Caesar_cypher This is what I was suggesting, which appears to be the same as the third method (Albam) of the Temurah cypher. Thanks, Ash http://www.ashleysheridan.co.uk
Re[2]: [PHP] preg_replace: avoiding double replacements
Hello Jim, That might work for that particular example, but I have utf-8 strings containing different characters of different alphabets, so neither str_replace nor strtr work... Thanks! -- 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: Jim Lucas li...@cmsws.com To: Andre Polykanine an...@oire.org Date: Tuesday, May 18, 2010, 3:33:09 AM Subject: [PHP] preg_replace: avoiding double replacements Andre Polykanine wrote: Hello everyone, Sorry for bothering you again. Today I met a problem exactly described by a developer in users' notes that follow the preg_replace description in the manual: info at gratisrijden dot nl 02-Oct-2009 02:48 if you are using the preg_replace with arrays, the replacements will apply as subject for the patterns later in the array. This means replaced values can be replaced again. Example: ?php $text = 'We want to replace BOLD with the boldtag and OLDTAG with the newtag'; $patterns = array( '/BOLD/i', '/OLDTAG/i'); $replacements = array( 'boldtag', 'newtag'); echo preg_replace ($patterns, $replacements, $text); ? Output: We want to replace bnewtag with the bnewtagtag and newtag with the newtag Look what happend with BOLD. Is there any solution to this besides any two-step sophisticated trick like case changing? Thanks! -- 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 Well, for the example you gave, why use regex? Check this out as an example. plaintext?php $text = 'We want to replace BOLD with the boldtag and OLDTAG with the newtag'; $regex = array( '/BOLD/i', '/OLDTAG/i', ); $oldtags = array( 'BOLD', 'OLDTAG', ); $replacements = array( 'boldtag', 'newtag', ); # Original String echo $text.\n; # After regex is applied echo preg_replace($regex, $replacements, $text).\n; # After plain tag replacement happens echo str_replace($oldtags, $replacements, $text).\n; ? See if that works for you. -- Jim Lucas Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re[2]: [PHP] preg_replace: avoiding double replacements
On 18 May 2010 09:04, Andre Polykanine an...@oire.org wrote: [snip] Andre Polykanine wrote: Hello everyone, Sorry for bothering you again. Today I met a problem exactly described by a developer in users' notes that follow the preg_replace description in the manual: info at gratisrijden dot nl 02-Oct-2009 02:48 if you are using the preg_replace with arrays, the replacements will apply as subject for the patterns later in the array. This means replaced values can be replaced again. Example: ?php $text = 'We want to replace BOLD with the boldtag and OLDTAG with the newtag'; $patterns = array( '/BOLD/i', '/OLDTAG/i'); $replacements = array( 'boldtag', 'newtag'); echo preg_replace ($patterns, $replacements, $text); ? Output: We want to replace bnewtag with the bnewtagtag and newtag with the newtag Look what happend with BOLD. Is there any solution to this besides any two-step sophisticated trick like case changing? Thanks! Use better regexes: either match for word endings or use a delimiter in your markers (i.e. ###BOLD### instead of BOLD). Regards Peter -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 /hype -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re[4]: [PHP] preg_replace: avoiding double replacements
Hello Peter, Hm... I see I need to specify what I'm really doing. Actually, I need to change the letters in the text. It's a famous and ancient crypting method: you divide the alphabet making two parts, then you change the letters of one part with letters for other part (so A becomes N, B becomes O, etc., and vice versa). it works fine and slightly with strtr or str_replace... but only if the text is not in utf-8 and it doesn't contain any non-English letters such as Cyrillic what I need. What my regex does is the following: it sees an A, well it changes it to N; then it goes through the string and sees an N... what does it do? Surely, it changes it back to A! I hoped (in vain) that there exists a modifier preventing this behavior... but it seems that it's false( Thanks! -- 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: Peter Lind peter.e.l...@gmail.com To: Andre Polykanine an...@oire.org Date: Tuesday, May 18, 2010, 10:19:51 AM Subject: [PHP] preg_replace: avoiding double replacements On 18 May 2010 09:04, Andre Polykanine an...@oire.org wrote: [snip] Andre Polykanine wrote: Hello everyone, Sorry for bothering you again. Today I met a problem exactly described by a developer in users' notes that follow the preg_replace description in the manual: info at gratisrijden dot nl 02-Oct-2009 02:48 if you are using the preg_replace with arrays, the replacements will apply as subject for the patterns later in the array. This means replaced values can be replaced again. Example: ?php $text = 'We want to replace BOLD with the boldtag and OLDTAG with the newtag'; $patterns = array( '/BOLD/i', '/OLDTAG/i'); $replacements = array( 'boldtag', 'newtag'); echo preg_replace ($patterns, $replacements, $text); ? Output: We want to replace bnewtag with the bnewtagtag and newtag with the newtag Look what happend with BOLD. Is there any solution to this besides any two-step sophisticated trick like case changing? Thanks! Use better regexes: either match for word endings or use a delimiter in your markers (i.e. ###BOLD### instead of BOLD). Regards Peter -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 /hype -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re[4]: [PHP] preg_replace: avoiding double replacements
On 18 May 2010 12:35, Andre Polykanine an...@oire.org wrote: Hello Peter, Hm... I see I need to specify what I'm really doing. Actually, I need to change the letters in the text. It's a famous and ancient crypting method: you divide the alphabet making two parts, then you change the letters of one part with letters for other part (so A becomes N, B becomes O, etc., and vice versa). it works fine and slightly with strtr or str_replace... but only if the text is not in utf-8 and it doesn't contain any non-English letters such as Cyrillic what I need. What my regex does is the following: it sees an A, well it changes it to N; then it goes through the string and sees an N... what does it do? Surely, it changes it back to A! I hoped (in vain) that there exists a modifier preventing this behavior... but it seems that it's false( Thanks! Hmmm, what comes to mind is using your string as an array and translating one character after another, building your output string using a lookup table. Not entirely sure how that will play with utf8 characters, you'd have to try and see. I don't think you'll get any of PHPs string functions to do the work for you - they'll do the job in serial, not parallel. Regards Peter -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 /hype -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re[4]: [PHP] preg_replace: avoiding double replacements
On Tue, 2010-05-18 at 13:09 +0200, Peter Lind wrote: On 18 May 2010 12:35, Andre Polykanine an...@oire.org wrote: Hello Peter, Hm... I see I need to specify what I'm really doing. Actually, I need to change the letters in the text. It's a famous and ancient crypting method: you divide the alphabet making two parts, then you change the letters of one part with letters for other part (so A becomes N, B becomes O, etc., and vice versa). it works fine and slightly with strtr or str_replace... but only if the text is not in utf-8 and it doesn't contain any non-English letters such as Cyrillic what I need. What my regex does is the following: it sees an A, well it changes it to N; then it goes through the string and sees an N... what does it do? Surely, it changes it back to A! I hoped (in vain) that there exists a modifier preventing this behavior... but it seems that it's false( Thanks! Hmmm, what comes to mind is using your string as an array and translating one character after another, building your output string using a lookup table. Not entirely sure how that will play with utf8 characters, you'd have to try and see. I don't think you'll get any of PHPs string functions to do the work for you - they'll do the job in serial, not parallel. Regards Peter -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 /hype If you're wanting to use the Caesar cypher (for that's what it is) then why not just modify the entire string, character by character, to use a character code n characters ahead. For example, a capital A is ascii 65, you want to change it to an N to add 14 to that. Just keep n the same throughout and it's easy to convert back. Thanks, Ash http://www.ashleysheridan.co.uk
Re: Re[4]: [PHP] preg_replace: avoiding double replacements
On 18 May 2010 13:32, Ashley Sheridan a...@ashleysheridan.co.uk wrote: On Tue, 2010-05-18 at 13:09 +0200, Peter Lind wrote: On 18 May 2010 12:35, Andre Polykanine an...@oire.org wrote: Hello Peter, Hm... I see I need to specify what I'm really doing. Actually, I need to change the letters in the text. It's a famous and ancient crypting method: you divide the alphabet making two parts, then you change the letters of one part with letters for other part (so A becomes N, B becomes O, etc., and vice versa). it works fine and slightly with strtr or str_replace... but only if the text is not in utf-8 and it doesn't contain any non-English letters such as Cyrillic what I need. What my regex does is the following: it sees an A, well it changes it to N; then it goes through the string and sees an N... what does it do? Surely, it changes it back to A! I hoped (in vain) that there exists a modifier preventing this behavior... but it seems that it's false( Thanks! Hmmm, what comes to mind is using your string as an array and translating one character after another, building your output string using a lookup table. Not entirely sure how that will play with utf8 characters, you'd have to try and see. I don't think you'll get any of PHPs string functions to do the work for you - they'll do the job in serial, not parallel. Regards Peter -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 /hype If you're wanting to use the Caesar cypher (for that's what it is) then why not just modify the entire string, character by character, to use a character code n characters ahead. For example, a capital A is ascii 65, you want to change it to an N to add 14 to that. Just keep n the same throughout and it's easy to convert back. Thanks, Ash http://www.ashleysheridan.co.uk You probably overlooked the part where the OP points out he's not using ascii but utf8. If it was just ascii, using str_rot13() would be the weapon of choice I'd say (note that adding 14 to every character of an ascii string will turn lots of it into gibberish - you have to wrap round when you reach a certain point). Regards Peter -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 /hype -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re[4]: [PHP] preg_replace: avoiding double replacements
On Tue, 2010-05-18 at 13:46 +0200, Peter Lind wrote: On 18 May 2010 13:32, Ashley Sheridan a...@ashleysheridan.co.uk wrote: On Tue, 2010-05-18 at 13:09 +0200, Peter Lind wrote: On 18 May 2010 12:35, Andre Polykanine an...@oire.org wrote: Hello Peter, Hm... I see I need to specify what I'm really doing. Actually, I need to change the letters in the text. It's a famous and ancient crypting method: you divide the alphabet making two parts, then you change the letters of one part with letters for other part (so A becomes N, B becomes O, etc., and vice versa). it works fine and slightly with strtr or str_replace... but only if the text is not in utf-8 and it doesn't contain any non-English letters such as Cyrillic what I need. What my regex does is the following: it sees an A, well it changes it to N; then it goes through the string and sees an N... what does it do? Surely, it changes it back to A! I hoped (in vain) that there exists a modifier preventing this behavior... but it seems that it's false( Thanks! Hmmm, what comes to mind is using your string as an array and translating one character after another, building your output string using a lookup table. Not entirely sure how that will play with utf8 characters, you'd have to try and see. I don't think you'll get any of PHPs string functions to do the work for you - they'll do the job in serial, not parallel. Regards Peter -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 /hype If you're wanting to use the Caesar cypher (for that's what it is) then why not just modify the entire string, character by character, to use a character code n characters ahead. For example, a capital A is ascii 65, you want to change it to an N to add 14 to that. Just keep n the same throughout and it's easy to convert back. Thanks, Ash http://www.ashleysheridan.co.uk You probably overlooked the part where the OP points out he's not using ascii but utf8. If it was just ascii, using str_rot13() would be the weapon of choice I'd say (note that adding 14 to every character of an ascii string will turn lots of it into gibberish - you have to wrap round when you reach a certain point). Regards Peter -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 /hype I gave the example as Ascii because I knew the code for A off the top of my head, I don't see a reason why it won't work for utf, the characters still have incremental codes. Also, is gibberish really an issue to worry about? The Caesar cypher is already rendering the string unreadable. Thanks, Ash http://www.ashleysheridan.co.uk
Re: Re[4]: [PHP] preg_replace: avoiding double replacements
On 18 May 2010 13:43, Ashley Sheridan a...@ashleysheridan.co.uk wrote: On Tue, 2010-05-18 at 13:46 +0200, Peter Lind wrote: On 18 May 2010 13:32, Ashley Sheridan a...@ashleysheridan.co.uk wrote: On Tue, 2010-05-18 at 13:09 +0200, Peter Lind wrote: On 18 May 2010 12:35, Andre Polykanine an...@oire.org wrote: Hello Peter, Hm... I see I need to specify what I'm really doing. Actually, I need to change the letters in the text. It's a famous and ancient crypting method: you divide the alphabet making two parts, then you change the letters of one part with letters for other part (so A becomes N, B becomes O, etc., and vice versa). it works fine and slightly with strtr or str_replace... but only if the text is not in utf-8 and it doesn't contain any non-English letters such as Cyrillic what I need. What my regex does is the following: it sees an A, well it changes it to N; then it goes through the string and sees an N... what does it do? Surely, it changes it back to A! I hoped (in vain) that there exists a modifier preventing this behavior... but it seems that it's false( Thanks! Hmmm, what comes to mind is using your string as an array and translating one character after another, building your output string using a lookup table. Not entirely sure how that will play with utf8 characters, you'd have to try and see. I don't think you'll get any of PHPs string functions to do the work for you - they'll do the job in serial, not parallel. Regards Peter -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 /hype If you're wanting to use the Caesar cypher (for that's what it is) then why not just modify the entire string, character by character, to use a character code n characters ahead. For example, a capital A is ascii 65, you want to change it to an N to add 14 to that. Just keep n the same throughout and it's easy to convert back. Thanks, Ash http://www.ashleysheridan.co.uk You probably overlooked the part where the OP points out he's not using ascii but utf8. If it was just ascii, using str_rot13() would be the weapon of choice I'd say (note that adding 14 to every character of an ascii string will turn lots of it into gibberish - you have to wrap round when you reach a certain point). Regards Peter -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 /hype I gave the example as Ascii because I knew the code for A off the top of my head, I don't see a reason why it won't work for utf, the characters still have incremental codes. Also, is gibberish really an issue to worry about? The Caesar cypher is already rendering the string unreadable. You normally want output in the same range that you encode from (i.e. you're remapping within the alphabet, not within the entire range of printable characters) if you're doing a caesar/rot13. Regards Peter -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 /hype -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re[6]: [PHP] preg_replace: avoiding double replacements
Hello Peter, Good point. And more than that, I make a decrypting script, also... so gibberish defenitely is an issue) -- 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: Peter Lind peter.e.l...@gmail.com To: a...@ashleysheridan.co.uk a...@ashleysheridan.co.uk Date: Tuesday, May 18, 2010, 3:00:56 PM Subject: [PHP] preg_replace: avoiding double replacements On 18 May 2010 13:43, Ashley Sheridan a...@ashleysheridan.co.uk wrote: On Tue, 2010-05-18 at 13:46 +0200, Peter Lind wrote: On 18 May 2010 13:32, Ashley Sheridan a...@ashleysheridan.co.uk wrote: On Tue, 2010-05-18 at 13:09 +0200, Peter Lind wrote: On 18 May 2010 12:35, Andre Polykanine an...@oire.org wrote: Hello Peter, Hm... I see I need to specify what I'm really doing. Actually, I need to change the letters in the text. It's a famous and ancient crypting method: you divide the alphabet making two parts, then you change the letters of one part with letters for other part (so A becomes N, B becomes O, etc., and vice versa). it works fine and slightly with strtr or str_replace... but only if the text is not in utf-8 and it doesn't contain any non-English letters such as Cyrillic what I need. What my regex does is the following: it sees an A, well it changes it to N; then it goes through the string and sees an N... what does it do? Surely, it changes it back to A! I hoped (in vain) that there exists a modifier preventing this behavior... but it seems that it's false( Thanks! Hmmm, what comes to mind is using your string as an array and translating one character after another, building your output string using a lookup table. Not entirely sure how that will play with utf8 characters, you'd have to try and see. I don't think you'll get any of PHPs string functions to do the work for you - they'll do the job in serial, not parallel. Regards Peter -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 /hype If you're wanting to use the Caesar cypher (for that's what it is) then why not just modify the entire string, character by character, to use a character code n characters ahead. For example, a capital A is ascii 65, you want to change it to an N to add 14 to that. Just keep n the same throughout and it's easy to convert back. Thanks, Ash http://www.ashleysheridan.co.uk You probably overlooked the part where the OP points out he's not using ascii but utf8. If it was just ascii, using str_rot13() would be the weapon of choice I'd say (note that adding 14 to every character of an ascii string will turn lots of it into gibberish - you have to wrap round when you reach a certain point). Regards Peter -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 /hype I gave the example as Ascii because I knew the code for A off the top of my head, I don't see a reason why it won't work for utf, the characters still have incremental codes. Also, is gibberish really an issue to worry about? The Caesar cypher is already rendering the string unreadable. You normally want output in the same range that you encode from (i.e. you're remapping within the alphabet, not within the entire range of printable characters) if you're doing a caesar/rot13. Regards Peter -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 /hype -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_replace: avoiding double replacements
Hello everyone, Sorry for bothering you again. Today I met a problem exactly described by a developer in users' notes that follow the preg_replace description in the manual: info at gratisrijden dot nl 02-Oct-2009 02:48 if you are using the preg_replace with arrays, the replacements will apply as subject for the patterns later in the array. This means replaced values can be replaced again. Example: ?php $text = 'We want to replace BOLD with the boldtag and OLDTAG with the newtag'; $patterns = array( '/BOLD/i', '/OLDTAG/i'); $replacements = array( 'boldtag', 'newtag'); echo preg_replace ($patterns, $replacements, $text); ? Output: We want to replace bnewtag with the bnewtagtag and newtag with the newtag Look what happend with BOLD. Is there any solution to this besides any two-step sophisticated trick like case changing? Thanks! -- 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
Re: [PHP] preg_replace: avoiding double replacements
Andre Polykanine wrote: Hello everyone, Sorry for bothering you again. Today I met a problem exactly described by a developer in users' notes that follow the preg_replace description in the manual: info at gratisrijden dot nl 02-Oct-2009 02:48 if you are using the preg_replace with arrays, the replacements will apply as subject for the patterns later in the array. This means replaced values can be replaced again. Example: ?php $text = 'We want to replace BOLD with the boldtag and OLDTAG with the newtag'; $patterns = array( '/BOLD/i', '/OLDTAG/i'); $replacements = array( 'boldtag', 'newtag'); echo preg_replace ($patterns, $replacements, $text); ? Output: We want to replace bnewtag with the bnewtagtag and newtag with the newtag Look what happend with BOLD. Is there any solution to this besides any two-step sophisticated trick like case changing? Thanks! -- 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 Well, for the example you gave, why use regex? Check this out as an example. plaintext?php $text = 'We want to replace BOLD with the boldtag and OLDTAG with the newtag'; $regex = array( '/BOLD/i', '/OLDTAG/i', ); $oldtags = array( 'BOLD', 'OLDTAG', ); $replacements = array( 'boldtag', 'newtag', ); # Original String echo $text.\n; # After regex is applied echo preg_replace($regex, $replacements, $text).\n; # After plain tag replacement happens echo str_replace($oldtags, $replacements, $text).\n; ? See if that works for you. -- Jim Lucas Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php