Re: [PHP] preg_replace
Hi, On Fri, Nov 01, 2013 at 11:06:29AM -0400, leam hall wrote: Despite my best efforts to ignore preg_replace... Why? :) PHP Warning: preg_replace(): Delimiter must not be alphanumeric or backslash Thoughts? You are just using it wrong. http://us2.php.net/manual/en/regexp.reference.delimiters.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_replace question
I have several poisoned .js files on a server. I can use find to recursively find them and then use preg_replace to replace the string. However the string is filled with single quotes, semi-colons and a lot of other special characters. Will preg_relace(escapeshellarg($String),$replacement) work or do I need to go through the entire string and escape what needs to be escaped? --C -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace question
On 13/12/2012, at 10:08 AM, Curtis Maurand cur...@maurand.com wrote: On 12/12/2012 3:47 PM, Maciek Sokolewicz wrote: On 12-12-2012 21:10, Curtis Maurand wrote: On 12/12/2012 12:00 PM, Maciek Sokolewicz wrote: On 12-12-2012 17:11, Curtis Maurand wrote: First of all, why do you want to use preg_replace when you're not actually using regular expressions??? Use str_replace or stri_replace instead. Aside from that, escapeshellarg() escapes strings for use in shell execution. Perl Regexps are not shell commands. It's like using mysqli_real_escape_string() to escape arguments for URLs. That doesn't compute, just like your way doesn't either. If you DO wish to escape arguments for a regular expression, use preg_quote instead, that's what it's there for. But first, reconsider using preg_replace, since I honestly don't think you need it at all if the way you've posted (preg_replace(escapeshellarg($string),$replacement)) is the way you want to use it. Thanks for your response. I'm open to to using str_replace. no issue there. my main question was how to properly get a string of javascript into a string that could then be processed. I'm not sure I can just put that in quotes and have it work.There are colons, ,, semicolons, and doublequotes. Do I just need to rifle through the string and escape the reserved characters or is there a function for that? --C Why do you want to escape them? There are no reserved characters in the case of str_replace. You don't have to put anything in quotes. For example: $string = 'This is a string with various supposedly reserved ``\\ _- characters' echo str_replace('supposedly', 'imaginary', $string) would return: This is a string with imaginary reserved ``\\- characters So... why do you want to escape these characters? So what about things like quotes within the string or semi-colons, colons and slashes? Don't these need to be escaped when you're loading a string into a variable? ;document.write('iframe width=50 height=50 style=width:100px;height:100px;position:absolute;left:-100px;top:0; src=http://nrwhuejbd.freewww.com/34e2b2349bdf29216e455cbc7b6491aa.cgi??8;/iframe'); I need to enclose this entire string and replace it with Thanks The only thing you have to worry about is quotes characters. Assuming you're running 5.3+, just use now docs (http://php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc). $String = 'STRING' ;document.write('iframe width=50 height=50 style=width:100px;height:100px;position:absolute;left:-100px;top:0; src=http://nrwhuejbd.freewww.com/34e2b2349bdf29216e455cbc7b6491aa.cgi??8;/iframe'); STRING; --- Simon Welsh Admin of http://simon.geek.nz/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace question
Am 24.01.2011 18:08, schrieb Alex Nikitin: If you declare your arrays, and set k to 0 first, put quotes around array values and use the correct limit (you can default to -1), you will get results, here is code and example (hopefully this helps you) ?php function internal_links($str, $links, $limit=-1) { $pattern=array(); $replace=array(); $k=0; foreach($links AS $link){ $pattern[$k] = ~\b({$link['phrase']})\b~i; $replace[$k] = 'a href='.$link['link'].'\\1/a'; $k++; } return preg_replace($pattern,$replace,$str, $limit); } echo internal_links(süße knuffige Beagle Welpen ab sofort, array(array('phrase'=beagle, 'link'=http://google.com;),array('phrase'=welpen, 'link'=http://wolframalpha.com;)), -1); Output: süße knuffigea href=http://google.com;Beagle/a a href= http://wolframalpha.com;Welpen/a ab ~Alex Hello, thank you all for your help. It seems that I am building the array wrong. Your code works with that array: $internal_links = array(array('phrase'=beagle, 'link'=http://google.com;),array('phrase'=welpen, 'link'=http://wolframalpha.com;)); I am pulling the data out of a DB and am using this code: while ($row = mysql_fetch_object($result)){ $internal_links[$row-ID]['phrase'] = $row-phrase; $internal_links[$row-ID]['link'] = $row-link; } You build the array different, could you help me to adapt this on my code? I tried $internal_links['phrase'][] as well, but that did not help either. Thank you for any help, Merlin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace question
Am 25.01.2011 12:31, schrieb Merlin Morgenstern: Am 24.01.2011 18:08, schrieb Alex Nikitin: If you declare your arrays, and set k to 0 first, put quotes around array values and use the correct limit (you can default to -1), you will get results, here is code and example (hopefully this helps you) ?php function internal_links($str, $links, $limit=-1) { $pattern=array(); $replace=array(); $k=0; foreach($links AS $link){ $pattern[$k] = ~\b({$link['phrase']})\b~i; $replace[$k] = 'a href='.$link['link'].'\\1/a'; $k++; } return preg_replace($pattern,$replace,$str, $limit); } echo internal_links(süße knuffige Beagle Welpen ab sofort, array(array('phrase'=beagle, 'link'=http://google.com;),array('phrase'=welpen, 'link'=http://wolframalpha.com;)), -1); Output: süße knuffigea href=http://google.com;Beagle/a a href= http://wolframalpha.com;Welpen/a ab ~Alex Hello, thank you all for your help. It seems that I am building the array wrong. Your code works with that array: $internal_links = array(array('phrase'=beagle, 'link'=http://google.com;),array('phrase'=welpen, 'link'=http://wolframalpha.com;)); I am pulling the data out of a DB and am using this code: while ($row = mysql_fetch_object($result)){ $internal_links[$row-ID]['phrase'] = $row-phrase; $internal_links[$row-ID]['link'] = $row-link; } You build the array different, could you help me to adapt this on my code? I tried $internal_links['phrase'][] as well, but that did not help either. Thank you for any help, Merlin HI Again :-) the building of my array seems fine. Here is what goes wrong: If you use this array: $internal_links = array(array('phrase'=Beagle Welpen, 'link'=http://wolframalpha.com;), array('phrase'=Welpen, 'link'=http://google.com;)); Then it will fail as well. This is because the function will replace Beagle Welpen with the hyperlink and after that replace the word welpen inside the hyperlink again with a hyperlink. Is there a function which will not start looking for the words at the beginnning of the text for each replacement, but simply continue where it did the last replacement? Thank you for any help, Merlin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace question
On 25 January 2011 12:04, Merlin Morgenstern merli...@fastmail.fm wrote: Am 25.01.2011 12:31, schrieb Merlin Morgenstern: Am 24.01.2011 18:08, schrieb Alex Nikitin: If you declare your arrays, and set k to 0 first, put quotes around array values and use the correct limit (you can default to -1), you will get results, here is code and example (hopefully this helps you) ?php function internal_links($str, $links, $limit=-1) { $pattern=array(); $replace=array(); $k=0; foreach($links AS $link){ $pattern[$k] = ~\b({$link['phrase']})\b~i; $replace[$k] = 'a href='.$link['link'].'\\1/a'; $k++; } return preg_replace($pattern,$replace,$str, $limit); } echo internal_links(süße knuffige Beagle Welpen ab sofort, array(array('phrase'=beagle, 'link'=http://google.com;),array('phrase'=welpen, 'link'=http://wolframalpha.com;)), -1); Output: süße knuffigea href=http://google.com;Beagle/a a href= http://wolframalpha.com;Welpen/a ab ~Alex Hello, thank you all for your help. It seems that I am building the array wrong. Your code works with that array: $internal_links = array(array('phrase'=beagle, 'link'=http://google.com;),array('phrase'=welpen, 'link'=http://wolframalpha.com;)); I am pulling the data out of a DB and am using this code: while ($row = mysql_fetch_object($result)){ $internal_links[$row-ID]['phrase'] = $row-phrase; $internal_links[$row-ID]['link'] = $row-link; } You build the array different, could you help me to adapt this on my code? I tried $internal_links['phrase'][] as well, but that did not help either. Thank you for any help, Merlin HI Again :-) the building of my array seems fine. Here is what goes wrong: If you use this array: $internal_links = array(array('phrase'=Beagle Welpen, 'link'=http://wolframalpha.com;), array('phrase'=Welpen, 'link'=http://google.com;)); Then it will fail as well. This is because the function will replace Beagle Welpen with the hyperlink and after that replace the word welpen inside the hyperlink again with a hyperlink. Is there a function which will not start looking for the words at the beginnning of the text for each replacement, but simply continue where it did the last replacement? Thank you for any help, Merlin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php The solution I've used in the past for this sort of issue (recursive replacements when not wanted) is to replace each known part with a unique placeholder. Once the initial data has been analysed and the placeholders are in place, then replace the placeholders with the correct value. So, rather than ... $internal_links = array ( array('phrase'=Beagle Welpen, 'link'=http://wolframalpha.com;), array('phrase'=Welpen, 'link'=http://google.com;) ); Use ... $internal_links = array ( array('phrase'='Beagle Welpen', 'link'='_RAQ_TAG_1_'), array('phrase'='Welpen','link'='_RAQ_TAG_2_'), array('phrase'='_RAQ_TAG_1_''link'='http://wolframalpha.com'), array('phrase'='_RAQ_TAG_2_''link'='http://google.com'), ); By keeping them in the above order, each phrase will be replaced in the same way. Obviously, if your text includes _RAQ_TAG_1_ or _RAQ_TAG_2_ then you will have to use more appropriate tags. Richard. -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace question
$internal_links=array(); I prefer to init arrays, it also avoids unnecessary notices, and sometimes weird results, but either one of those while loops should make the desired array while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { array_push($internal_links, array('phrase'=$row['phrase'], 'link'=$row['link'])); } or while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $internal_links[] = array('phrase'=$row['phrase'], 'link'=$row['link']); } or while($row = mysql_fetch_object($result)) { $internal_links[] = array('phrase'=$row-phrase, 'link'=$row-link); } (you can figure out how to do it with array_push if you choose to, but you get the general idea) ~ Alex On Jan 25, 2011 6:35 AM, Merlin Morgenstern merli...@fastmail.fm wrote: Am 24.01.2011 18:08, schrieb Alex Nikitin: If you declare your arrays, and set k to 0 first, put quotes around array values and use the correct limit (you can default to -1), you will get results, here is code and example (hopefully this helps you) ?php function internal_links($str, $links, $limit=-1) { $pattern=array(); $replace=array(); $k=0; foreach($links AS $link){ $pattern[$k] = ~\b({$link['phrase']})\b~i; $replace[$k] = 'a href='.$link['link'].'\\1/a'; $k++; } return preg_replace($pattern,$replace,$str, $limit); } echo internal_links(süße knuffige Beagle Welpen ab sofort, array(array('phrase'=beagle, 'link'=http://google.com;),array('phrase'=welpen, 'link'=http://wolframalpha.com;)), -1); Output: süße knuffigea href=http://google.com;Beagle/a a href= http://wolframalpha.com;Welpen/a ab ~Alex Hello, thank you all for your help. It seems that I am building the array wrong. Your code works with that array: $internal_links = array(array('phrase'=beagle, 'link'=http://google.com;),array('phrase'=welpen, 'link'=http://wolframalpha.com;)); I am pulling the data out of a DB and am using this code: while ($row = mysql_fetch_object($result)){ $internal_links[$row-ID]['phrase'] = $row-phrase; $internal_links[$row-ID]['link'] = $row-link; } You build the array different, could you help me to adapt this on my code? I tried $internal_links['phrase'][] as well, but that did not help either. Thank you for any help, Merlin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_replace question
Hi there, I am trying to replace certain words inside a text with php. Unfortunatelly my function is creating invalid html as output. For example the words beagle and welpen have to be replaced inside this text: süße knuffige Beagle Welpen ab sofort My result looks like this: zwei süße knuffige a href=/bsp/hunde,beagleBeagle a href=/bsp/hundeWelpen/a/a The problem is, that my function is not closing the href tag before it starts to replace the next item. Here is the code: // create internal links function internal_links($str, $links, $limit) { foreach($links AS $link){ $pattern[$k] = ~\b($link[phrase])\b~i; $replace[$k] = 'a href='.$link[link].'\\1/a'; $k++; } return preg_replace($pattern,$replace,$str, $limit); } I I could not find a way to fix this and I would be happy for some help. Thank you in advance! Merlin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace question
Without seeing the code that creates the arrays, it's tough to see the problem. It looks like the first replacement is catching Beagle Welpen entirely since the closing /a tag gets placed after Welpen. Then the second replacement does just Welpen. Also, you should have quotes around link when building the $replace[] entry since the array access is outside quotes. Finally, you don't need $k here at all. // create internal links function internal_links($str, $links, $limit) { foreach($links AS $link){ $pattern[] = ~\b($link[phrase])\b~i; $replace[] = 'a href='.$link['link'].'\\1/a'; } return preg_replace($pattern,$replace,$str, $limit); } David
Re: [PHP] preg_replace question
If you declare your arrays, and set k to 0 first, put quotes around array values and use the correct limit (you can default to -1), you will get results, here is code and example (hopefully this helps you) ?php function internal_links($str, $links, $limit=-1) { $pattern=array(); $replace=array(); $k=0; foreach($links AS $link){ $pattern[$k] = ~\b({$link['phrase']})\b~i; $replace[$k] = 'a href='.$link['link'].'\\1/a'; $k++; } return preg_replace($pattern,$replace,$str, $limit); } echo internal_links(süße knuffige Beagle Welpen ab sofort, array(array('phrase'=beagle, 'link'=http://google.com;),array('phrase'=welpen, 'link'=http://wolframalpha.com;)), -1); Output: süße knuffige a href=http://google.com;Beagle/a a href= http://wolframalpha.com;Welpen/a ab ~Alex
Re: [PHP] preg_replace question
On 1/24/2011 8:00 AM, Merlin Morgenstern wrote: Hi there, I am trying to replace certain words inside a text with php. Unfortunatelly my function is creating invalid html as output. For example the words beagle and welpen have to be replaced inside this text: süße knuffige Beagle Welpen ab sofort My result looks like this: zwei süße knuffige a href=/bsp/hunde,beagleBeagle a href=/bsp/hundeWelpen/a/a The problem is, that my function is not closing the href tag before it starts to replace the next item. Here is the code: // create internal links function internal_links($str, $links, $limit) { foreach($links AS $link){ $pattern[$k] = ~\b($link[phrase])\b~i; $replace[$k] = 'a href='.$link[link].'\\1/a'; $k++; } return preg_replace($pattern,$replace,$str, $limit); } I I could not find a way to fix this and I would be happy for some help. Thank you in advance! Merlin Do you have control over the building of the initial phrase = link assoc? If so, reverse the order of these two items. Jim Lucas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_replace insert newline
$string = 'text with no newline'; $pattern = '/(.*)/'; $replacement = '${1}XX\nNext line'; $string = preg_replace($pattern, $replacement, $string); echo $string; Outputs: text with no newlineXX\nNext line Instead of: text with no newlineXX Next line How does one insert a newline with preg_replace? Thanks -- 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
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
[PHP] preg_replace help
$fixSrch[] = '/\n/'; $fixRplc[] = '[br]'; is what I need except I want it to leave anything between [code] and [/code] alone. I figured it out before but with element /element but I don't even remember what I was working on when I did that and I can't for the life of me find it now. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace help
Michael A. Peters wrote on 26/01/2010 14:18: $fixSrch[] = '/\n/'; $fixRplc[] = '[br]'; is what I need except I want it to leave anything between [code] and [/code] alone. I figured it out before but with element /element but I don't even remember what I was working on when I did that and I can't for the life of me find it now. Just use the function nl2br() If you wanna match \n, you need to add a backslash before the backslash: \\n -- Kind regards Kim Emax - masterminds.dk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace help
Kim Madsen wrote: Michael A. Peters wrote on 26/01/2010 14:18: $fixSrch[] = '/\n/'; $fixRplc[] = '[br]'; is what I need except I want it to leave anything between [code] and [/code] alone. I figured it out before but with element /element but I don't even remember what I was working on when I did that and I can't for the life of me find it now. Just use the function nl2br() If you wanna match \n, you need to add a backslash before the backslash: \\n No, I do NOT want to use nl2br. For one thing, nl2br isn't xml safe so I'd have to do another preg_replace to fix that. My bbcode parser is xml safe. For another thing, my bbcode parser doesn't like html in its input, and this needs to be done before the parser. '/\n/','[br]' works exactly as I want it to right now except I do not want it replace the newlines inside [code][/code] as that messes up the syntax highlighting that is then done one the code block. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace help
Michael A. Peters wrote: Kim Madsen wrote: Michael A. Peters wrote on 26/01/2010 14:18: $fixSrch[] = '/\n/'; $fixRplc[] = '[br]'; is what I need except I want it to leave anything between [code] and [/code] alone. I figured it out before but with element /element but I don't even remember what I was working on when I did that and I can't for the life of me find it now. Just use the function nl2br() If you wanna match \n, you need to add a backslash before the backslash: \\n No, I do NOT want to use nl2br. For one thing, nl2br isn't xml safe so I'd have to do another preg_replace to fix that. My bbcode parser is xml safe. For another thing, my bbcode parser doesn't like html in its input, and this needs to be done before the parser. '/\n/','[br]' works exactly as I want it to right now except I do not want it replace the newlines inside [code][/code] as that messes up the syntax highlighting that is then done one the code block. I got it, though it may not be the most elegant way. I split the input up into array and do the preg_replace on array elements that are not inside [code]. Seems to work, and also solves the other problem (other problem being proper application of strip_tags except inside [code]. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace anything that isn't WORD
On Sat, Aug 22, 2009 at 12:32 PM, ÈýÏÝÂÔdanondan...@gmail.com wrote: Lets assume I have the string cats i saw a cat and a dog i want to strip everything except cat and dog so the result will be catcatdog, using preg_replace. I've tried something like /[^(dog|cat)]+/ but no success What should I do? Lot's of ways to skin this cat/dog. What's wrong with exploding the string using spaces and then walking the array looking for cat and dog while assembling the resultant string? Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace anything that isn't WORD
Use preg_replace_callback instead! preg_replace_callback is better performance than preg_replace with /e. - code $str=cats i saw a cat and a dog; $str1=preg_replace_callback(/(dog|cat|.)/is,call_replace,$str); echo $str.BR/; echo $str1; function call_replace($match){ if(in_array($match[0],array('cat','dog'))) return $match[0]; else return ; } 2009/8/24 tedd tedd.sperl...@gmail.com: On Sat, Aug 22, 2009 at 12:32 PM, “•ÈýÏÝ“•ÂÔdanondan...@gmail.com wrote: Lets assume I have the string cats i saw a cat and a dog i want to strip everything except cat and dog so the result will be catcatdog, using preg_replace. I've tried something like /[^(dog|cat)]+/ but no success What should I do? Lot's of ways to skin this cat/dog. What's wrong with exploding the string using spaces and then walking the array looking for cat and dog while assembling the resultant string? Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace anything that isn't WORD
hack988 hack988 wrote: Use preg_replace_callback instead! preg_replace_callback is better performance than preg_replace with /e. - code $str=cats i saw a cat and a dog; $str1=preg_replace_callback(/(dog|cat|.)/is,call_replace,$str); echo $str.BR/; echo $str1; function call_replace($match){ if(in_array($match[0],array('cat','dog'))) return $match[0]; else return ; } 2009/8/24 tedd tedd.sperl...@gmail.com: On Sat, Aug 22, 2009 at 12:32 PM, “•ÈýÏÝ“•ÂÔdanondan...@gmail.com wrote: Lets assume I have the string cats i saw a cat and a dog i want to strip everything except cat and dog so the result will be catcatdog, using preg_replace. I've tried something like /[^(dog|cat)]+/ but no success What should I do? Lot's of ways to skin this cat/dog. What's wrong with exploding the string using spaces and then walking the array looking for cat and dog while assembling the resultant string? Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Certain posts of mine seem to get sucked into a black hole and I never see theme. Maybe because I use this list as a newsgroup? Anyway, what I posted before: Match everything but only replace the backreference for the words: $s = cats i saw a cat and a dog; $r = preg_replace('#.*?(cat|dog).*?#', '\1', $s); -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace anything that isn't WORD
For the record Shawn: I received your previous post from Aug 22 and I think that it is the best solution. Jonathan On Tue, Aug 25, 2009 at 12:41 AM, Shawn McKenzienos...@mckenzies.net wrote: hack988 hack988 wrote: Use preg_replace_callback instead! preg_replace_callback is better performance than preg_replace with /e. - code $str=cats i saw a cat and a dog; $str1=preg_replace_callback(/(dog|cat|.)/is,call_replace,$str); echo $str.BR/; echo $str1; function call_replace($match){ if(in_array($match[0],array('cat','dog'))) return $match[0]; else return ; } 2009/8/24 tedd tedd.sperl...@gmail.com: On Sat, Aug 22, 2009 at 12:32 PM, “•ÈýÏÝ“•ÂÔdanondan...@gmail.com wrote: Lets assume I have the string cats i saw a cat and a dog i want to strip everything except cat and dog so the result will be catcatdog, using preg_replace. I've tried something like /[^(dog|cat)]+/ but no success What should I do? Lot's of ways to skin this cat/dog. What's wrong with exploding the string using spaces and then walking the array looking for cat and dog while assembling the resultant string? Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Certain posts of mine seem to get sucked into a black hole and I never see theme. Maybe because I use this list as a newsgroup? Anyway, what I posted before: Match everything but only replace the backreference for the words: $s = cats i saw a cat and a dog; $r = preg_replace('#.*?(cat|dog).*?#', '\1', $s); -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_replace anything that isn't WORD
Lets assume I have the string cats i saw a cat and a dog i want to strip everything except cat and dog so the result will be catcatdog, using preg_replace. I've tried something like /[^(dog|cat)]+/ but no success What should I do? -- Use ROT26 for best security
Re: [PHP] preg_replace with UTF-8
On Mon, Jul 6, 2009 at 4:54 AM, SleePy sleepingkil...@gmail.com wrote: I seem to be having a minor issue with preg_replace not working as expected when using UTF-8 strings. So far I have found out that \w doesn't seem to be detecting UTF-8 strings. This is my test php file: ?php $data = 'ooo'; echo 'Data before: ', $data, 'br /'; $data = preg_replace('~([\w\.]{6})~u', '$1 ', $data); echo 'Data After: ', $data; // UTF-8 Test $data = 'ффф'; echo 'hr /Data before: ', $data, 'br /'; $data = preg_replace('~([\w\.]{6})~u', '$1 ', $data); echo 'Data After: ', $data; ? I would expect it to be: Data before: ooo Data After: oo oo oo o --- Data before: ффф Data After: фф фф фф ф But what I get is: Data before: ooo Data After: oo oo oo o --- Data before: ффф Data After: ффф Did I go about this the wrong way or is this a php bug itself? I tested this in php 5.3, 5.2.9 and 6.0 (snapshot from a couple weeks ago) and received the same results. Did you tried mb_ereg_replace? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace with UTF-8
Thank you Andrew, That seems to break up UTF-8 strings. So from there I will play with it. On Jul 6, 2009, at 8:50 AM, Andrew Ballard wrote: On Sun, Jul 5, 2009 at 9:54 PM, SleePysleepingkil...@gmail.com wrote: I seem to be having a minor issue with preg_replace not working as expected when using UTF-8 strings. So far I have found out that \w doesn't seem to be detecting UTF-8 strings. This is my test php file: ?php $data = 'ooo'; echo 'Data before: ', $data, 'br /'; $data = preg_replace('~([\w\.]{6})~u', '$1 ', $data); echo 'Data After: ', $data; // UTF-8 Test $data = 'ффф'; echo 'hr /Data before: ', $data, 'br /'; $data = preg_replace('~([\w\.]{6})~u', '$1 ', $data); echo 'Data After: ', $data; ? I would expect it to be: Data before: ooo Data After: oo oo oo o --- Data before: ффф Data After: фф фф фф ф But what I get is: Data before: ooo Data After: oo oo oo o --- Data before: ффф Data After: ффф Did I go about this the wrong way or is this a php bug itself? I tested this in php 5.3, 5.2.9 and 6.0 (snapshot from a couple weeks ago) and received the same results. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php From the manual on PCRE syntax: A 'word' character is any letter or digit or the underscore character, that is, any character which can be part of a Perl 'word'. The definition of letters and digits is controlled by PCRE's character tables, and may vary if locale-specific matching is taking place. For example, in the 'fr' (French) locale, some character codes greater than 128 are used for accented letters, and these are matched by \w. These character type sequences can appear both inside and outside character classes. They each match one character of the appropriate type. If the current matching point is at the end of the subject string, all of them fail, since there is no character to match. I'm not sure if this is exactly what you want (or if it might let more things slip past than you intend), but try this: ?php $data = 'ooo'; echo 'Data before: ', $data, 'br /'; $data = preg_replace('~([\w\pL\.]{6})~u', '$1 ', $data); echo 'Data After: ', $data; // UTF-8 Test $data = 'ффф'; echo 'hr /Data before: ', $data, 'br /'; $data = preg_replace('~([\w\pL\.]{6})~u', '$1 ', $data); echo 'Data After: ', $data; ? Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_replace with UTF-8
I seem to be having a minor issue with preg_replace not working as expected when using UTF-8 strings. So far I have found out that \w doesn't seem to be detecting UTF-8 strings. This is my test php file: ?php $data = 'ooo'; echo 'Data before: ', $data, 'br /'; $data = preg_replace('~([\w\.]{6})~u', '$1 ', $data); echo 'Data After: ', $data; // UTF-8 Test $data = 'ффф'; echo 'hr /Data before: ', $data, 'br /'; $data = preg_replace('~([\w\.]{6})~u', '$1 ', $data); echo 'Data After: ', $data; ? I would expect it to be: Data before: ooo Data After: oo oo oo o --- Data before: ффф Data After: фф фф фф ф But what I get is: Data before: ooo Data After: oo oo oo o --- Data before: ффф Data After: ффф Did I go about this the wrong way or is this a php bug itself? I tested this in php 5.3, 5.2.9 and 6.0 (snapshot from a couple weeks ago) and received the same results. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_replace problem
This preg_replace() should simply replace all with amp; unless the value is already amp; But; if $value is simple a quote character [] I get quote. e.g., test = quote;testquote; Search string and replace works as it should in Regex_Coach. echo $value.'br /'; $value=preg_replace(%(?!amp;)%i, amp;, $value); echo $value; I tried using \x26 for the in the search string; didn't help. This seems too obvious to be a bug. Using php5.2.9 Al... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_replace
I bought PHP MySQL for DUMMIES and it shows me how to use special characters for pattern matching and I've figured out the basics of using preg_replace to replace pattern matches. What I am having trouble with, though, is figuring out how to replace anything that does not match the pattern. For example, I want to replace anything that is NOT an alphanumeric character. Any help would be appreciated. Thanks. Ben -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] preg_replace
Oh yeah - not sure if spaces are considered alphanumeric or not, but I need to keep spaces - replacing anything that is NOT a letter, a number or a space. Thanks again. -Original Message- From: Ben Miller [mailto:biprel...@gmail.com] Sent: Friday, June 05, 2009 2:09 AM To: php-general@lists.php.net Subject: [PHP] preg_replace I bought PHP MySQL for DUMMIES and it shows me how to use special characters for pattern matching and I've figured out the basics of using preg_replace to replace pattern matches. What I am having trouble with, though, is figuring out how to replace anything that does not match the pattern. For example, I want to replace anything that is NOT an alphanumeric character. Any help would be appreciated. Thanks. Ben -- 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: [PHP] preg_replace
Hey Ben, to replace everything thats not alphanumeric, use the following statement: $output = preg_replace('/[^[:alnum:]]/', '', $input); Greetings from Germany Marc PS: Spaces are not alphanumeric ;) Ben Miller wrote: Oh yeah - not sure if spaces are considered alphanumeric or not, but I need to keep spaces - replacing anything that is NOT a letter, a number or a space. Thanks again. -Original Message- From: Ben Miller [mailto:biprel...@gmail.com] Sent: Friday, June 05, 2009 2:09 AM To: php-general@lists.php.net Subject: [PHP] preg_replace I bought PHP MySQL for DUMMIES and it shows me how to use special characters for pattern matching and I've figured out the basics of using preg_replace to replace pattern matches. What I am having trouble with, though, is figuring out how to replace anything that does not match the pattern. For example, I want to replace anything that is NOT an alphanumeric character. Any help would be appreciated. Thanks. Ben -- Synchronize and share your files over the web for free http://bithub.net/ My Twitter feed http://twitter.com/MarcSteinert -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace() question
1. What is the overhead on preg_replace? Minimal. If you're looking for all the speed you can get, you'd probably be better off with an str* function though if you can find one. You'd have to be seriously after speed gains though. 2. Is there a better way to strip spaces and non alpha numerical characters from text strings? I suspect not... Have a look through the string functions. the ctype_* functions too. See if one fits your needs. -- Richard Heyes HTML5 Canvas graphing for Firefox, Chrome, Opera and Safari: http://www.rgraph.net (Updated March 14th) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace() question
1. What is the overhead on preg_replace? it really depends on your operation. when you think it can be done using str* functions then go for it as they are much faster than preg* functions. 2. Is there a better way to strip spaces and non alpha numerical characters from text strings? I suspect not... maybe the Shadow does ??? if those characters are in the middle, preg_replace is the right function. virgil http://www.jampmark.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace() question
On Wed, 2009-03-18 at 22:55 +0800, Virgilio Quilario wrote: 1. What is the overhead on preg_replace? it really depends on your operation. when you think it can be done using str* functions then go for it as they are much faster than preg* functions. 2. Is there a better way to strip spaces and non alpha numerical characters from text strings? I suspect not... maybe the Shadow does ??? if those characters are in the middle, preg_replace is the right function. Unless you know how many, it's probably the right function even if they're at the front or end. preg_replace() is almost certainly faster (in this particular case) than making two function calls. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_replace() question
1. What is the overhead on preg_replace? 2. Is there a better way to strip spaces and non alpha numerical characters from text strings? I suspect not... maybe the Shadow does ??? :-D -- unheralded genius: A clean desk is the sign of a dull mind. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace() question
PJ wrote: 1. What is the overhead on preg_replace? Compared to what? If you write a 3 line regex, it's going to take some processing. 2. Is there a better way to strip spaces and non alpha numerical characters from text strings? I suspect not... maybe the Shadow does ??? For this, preg_replace is probably the right option. -- Postgresql php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace strange behaviour, duplicates
You know what's not supposed to be next in the second string, and that's the word Duo. Thank you Micah Gersten onShore Networks Internal Developer http://www.onshore.com Adz07 wrote: Problem is that a negative assertion assumes i know what is going to come after the match, but i don't. I am a bit stuck now :( Adz07 wrote: I am trying to nail down a bit of code for changing processor names depending on matches. Problem i am having is the replacement takes place then it seems to do it again replacing the text just replaced as there are similar matches afterwards. example (easier) $string = The new Intel Core 2 Duo T8300; $patterns = array(/Intel Core 2 Duo/,/Intel Core 2/); $replacements = array(Intel Core 2 Duo Processor Technology,Intel Core 2 Processor Technology); I would expect to get the following: The new Intel Core 2 Duo Processor Technology T8300 but i get The new Intel Core 2 Processor Technology Duo Processor Technology T8300 I can see why its doing it, reading the string in and making the replacement but then reading the string in for the next pattern, but i don't want it to do this. How do i stop preg_replace from reading in the same part of the string that has been replaced already? (it's a bad day! :( ) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace strange behaviour, duplicates
Problem is that a negative assertion assumes i know what is going to come after the match, but i don't. I am a bit stuck now :( Adz07 wrote: I am trying to nail down a bit of code for changing processor names depending on matches. Problem i am having is the replacement takes place then it seems to do it again replacing the text just replaced as there are similar matches afterwards. example (easier) $string = The new Intel Core 2 Duo T8300; $patterns = array(/Intel Core 2 Duo/,/Intel Core 2/); $replacements = array(Intel Core 2 Duo Processor Technology,Intel Core 2 Processor Technology); I would expect to get the following: The new Intel Core 2 Duo Processor Technology T8300 but i get The new Intel Core 2 Processor Technology Duo Processor Technology T8300 I can see why its doing it, reading the string in and making the replacement but then reading the string in for the next pattern, but i don't want it to do this. How do i stop preg_replace from reading in the same part of the string that has been replaced already? (it's a bad day! :( ) -- View this message in context: http://www.nabble.com/preg_replace-strange-behaviour%2C-duplicates-tp19001166p19027490.html Sent from the PHP - General mailing list archive at Nabble.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] preg_replace strange behaviour, duplicates
?php $string = The new Intel Core 2 Duo T8300; $name = Intel Core 2; $extra = Duo; $insert = Processor Technology; echo preg_replace(/({$name}{$extra}?)/, $1.$insert, $string); ? Simcha Younger -Original Message- From: Adz07 [mailto:[EMAIL PROTECTED] Sent: Monday, August 18, 2008 10:17 AM To: php-general@lists.php.net Subject: Re: [PHP] preg_replace strange behaviour, duplicates Problem is that a negative assertion assumes i know what is going to come after the match, but i don't. I am a bit stuck now :( Adz07 wrote: I am trying to nail down a bit of code for changing processor names depending on matches. Problem i am having is the replacement takes place then it seems to do it again replacing the text just replaced as there are similar matches afterwards. example (easier) $string = The new Intel Core 2 Duo T8300; $patterns = array(/Intel Core 2 Duo/,/Intel Core 2/); $replacements = array(Intel Core 2 Duo Processor Technology,Intel Core 2 Processor Technology); I would expect to get the following: The new Intel Core 2 Duo Processor Technology T8300 but i get The new Intel Core 2 Processor Technology Duo Processor Technology T8300 I can see why its doing it, reading the string in and making the replacement but then reading the string in for the next pattern, but i don't want it to do this. How do i stop preg_replace from reading in the same part of the string that has been replaced already? (it's a bad day! :( ) -- View this message in context: http://www.nabble.com/preg_replace-strange-behaviour%2C-duplicates-tp1900116 6p19027490.html Sent from the PHP - General mailing list archive at Nabble.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php No virus found in this incoming message. Checked by AVG - http://www.avg.com http://www.avg.com/ Version: 8.0.138 / Virus Database: 270.6.4/1617 - Release Date: 17/08/2008 12:58
[PHP] preg_replace strange behaviour, duplicates
I am trying to nail down a bit of code for changing processor names depending on matches. Problem i am having is the replacement takes place then it seems to do it again replacing the text just replaced as there are similar matches afterwards. example (easier) $string = The new Intel Core 2 Duo T8300; $patterns = array(/Intel Core 2 Duo/,/Intel Core 2/); $replacements = array(/Intel Core 2 Duo Processor Technology/,/Intel Core 2 Processor Technology/); I would expect to get the following: The new Intel Core 2 Duo Processor Technology T8300 but i get The new Intel Core 2 Processor Technology Duo Processor Technology T8300 I can see why its doing it, reading the string in and making the replacement but then reading the string in for the next pattern, but i don't want it to do this. How do i stop preg_replace from reading in the same part of the string that has been replaced already? (it's a bad day! :( ) -- View this message in context: http://www.nabble.com/preg_replace-strange-behaviour%2C-duplicates-tp19001166p19001166.html Sent from the PHP - General mailing list archive at Nabble.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace strange behaviour, duplicates
Take a look at the negative assertions on this page: http://us2.php.net/manual/en/regexp.reference.php Thank you, Micah Gersten onShore Networks Internal Developer http://www.onshore.com Adz07 wrote: I am trying to nail down a bit of code for changing processor names depending on matches. Problem i am having is the replacement takes place then it seems to do it again replacing the text just replaced as there are similar matches afterwards. example (easier) $string = The new Intel Core 2 Duo T8300; $patterns = array(/Intel Core 2 Duo/,/Intel Core 2/); $replacements = array(/Intel Core 2 Duo Processor Technology/,/Intel Core 2 Processor Technology/); I would expect to get the following: The new Intel Core 2 Duo Processor Technology T8300 but i get The new Intel Core 2 Processor Technology Duo Processor Technology T8300 I can see why its doing it, reading the string in and making the replacement but then reading the string in for the next pattern, but i don't want it to do this. How do i stop preg_replace from reading in the same part of the string that has been replaced already? (it's a bad day! :( ) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Preg_Replace $pattern syntax error
On Mon, Jun 9, 2008 at 8:07 PM, Graham Anderson [EMAIL PROTECTED] wrote: Hi How can I convert the regular expression: p\s+style=padding-left:\s+(\d+)px;(.*?)/p into a pattern that PHP will accept? [snip!] Change this: $pattern='p\s+style=padding-left:\s+(\d+)px;(.*?)/p'; To this: $pattern='/p\s+style=padding-left:\s+(\d+)px;(.*?)\/p/Uis'; This adds the delimiters (/) to the beginning and end of the string, and escapes the slash in the /p so it doesn't kick-out early, leaving trailing characters. After the ending delimiter, it tells preg_replace() to be Ungreedy, CASE-iNSENSITIVE, and to feel free to cross over newline boundaries (with the 's' modifier). From there, you can modify your regexp as needed. One of the absolute best resources for regexp's on the 'Net today, in my opinion, is here: http://www.regular-expressions.info/ It's even good for those of us who forget things years later ;-P -- /Daniel P. Brown Dedicated Servers - Intel 2.4GHz w/2TB bandwidth/mo. starting at just $59.99/mo. with no contract! Dedicated servers, VPS, and hosting from $2.50/mo. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Preg_Replace $pattern syntax error
Hi How can I convert the regular expression:p\s+style=padding-left: \s+(\d+)px;(.*?)/p into a pattern that PHP will accept? I am getting the error: Warning: preg_replace() [function.preg-replace]: Unknown modifier '(' in /Library/WebServer/Documents/tamagotchi/runtime/content/js/tiny_mce/ examples/tinymce_regex.php on line 24 I am very new to regular expressions and any help is appreciated. Something to do with escaping certain strings? G $html =EOB TinyMCE is a span style=font-style: italic;platform/span p style=text-align: center;We recommend a href=http://www.getfirefox.com target=_blankFirefox/a /p p style=padding-left: 30px;May the Regular Expression Find Me!/p EOB; $pattern='p\s+style=padding-left:\s+(\d+)px;(.*?)/p'; $replace= 'textformat indent=\\1\\2/textformat'; echo preg_replace($pattern,$replace, $html ); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Preg_Replace $pattern syntax error [solved]
I needed to add the ~ character as a delimiter. So, the below now works $pattern='~p\s+style=padding-left:\s+(\d+)px;(.*?)/p~'; G Hi How can I convert the regular expression:p\s+style=padding- left:\s+(\d+)px;(.*?)/p into a pattern that PHP will accept? I am getting the error: Warning: preg_replace() [function.preg-replace]: Unknown modifier '(' in /Library/WebServer/Documents/tamagotchi/runtime/content/js/ tiny_mce/examples/tinymce_regex.php on line 24 I am very new to regular expressions and any help is appreciated. Something to do with escaping certain strings? G $html =EOB TinyMCE is a span style=font-style: italic;platform/span p style=text-align: center;We recommend a href=http://www.getfirefox.com target=_blankFirefox/a /p p style=padding-left: 30px;May the Regular Expression Find Me!/p EOB; $pattern='p\s+style=padding-left:\s+(\d+)px;(.*?)/p'; $replace= 'textformat indent=\\1\\2/textformat'; echo preg_replace($pattern,$replace, $html );
Re: [PHP] preg_replace Question
Richard Luckhurst wrote: e.g $amount = $524.00 however only 4.00 is displayed in the %Amount field on the html page. I tried dropping the .00 from $amount to see if this might be a length issue and then %Amount was just 4 Am I doing something obviously wrong here? I have checked the php manual and I appear to be using preg_replace correctly. From the manual: replacement may contain references of the form \\n or (since PHP 4.0.4) $n, with the latter form being the preferred one If you use $amount ='\$524.00' instead of '$524.00', it'll work. /Per Jessen, Zürich -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_replace Question
Hi List I am trying to perform a number of replacements of place holders in an html page I am working on using preg_replace. I am stuck with a pronlem I can not work out and would appreciate some help. The code I have is as follows $html = preg_replace('/%Amount/',$amount,$html); $html is the source of a html page $amount is set earlier to a value read from a file When I view the html page the value of %Amount is not what I would expect. e.g $amount = $524.00 however only 4.00 is displayed in the %Amount field on the html page. I tried dropping the .00 from $amount to see if this might be a length issue and then %Amount was just 4 Am I doing something obviously wrong here? I have checked the php manual and I appear to be using preg_replace correctly. Regards, Richard Luckhurst Product Development Exodus Systems - Sydney, Australia. [EMAIL PROTECTED] Tel: (+612) 4751-9633 Fax: (+612) 4751-9644 Web: www.resmaster.com = Exodus Systems - Smarter Systems, Better Business = -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: PHP preg_replace help
Just use stripslashes() on your submitted data and forget about testing for magic_quotes. It's good practice anyhow. \ is not legit text regardless. haim Chaikin wrote: Hello, I am a beginner in PHP. I need help with the function preg_replace. I am trying to remove the backslashes ( \ ) from a string that is submitted by the user. It is submitted in a form but it adds \ before the quotation marks ( ). Will this change if I use the GET method instead of POST. If not can you please tell me how to use preg_replace to remove the backslashes. Thank You, Chaim Chaikin Novice PHP programmer Hotwire Band Website Administrator http://hotwire.totalh.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: PHP preg_replace help
Apologies if you already received this message, I tried to send it earlier from my webmail but it doesn't seem to have worked. Al wrote: Just use stripslashes() on your submitted data and forget about testing for magic_quotes. It's good practice anyhow. \ is not legit text regardless. Using stripslashes() on all submitted data is most certainly *not* good practice. If magic_quotes_gpc is later turned off or you're using one of the versions of PHP with buggy magic_quotes_gpc support then you can easily lose data. Reversing the effects of magic_quotes_gpc is far from trivial, there's lots of potential for subtle bugs, let alone completely forgetting about $_COOKIE. See my earlier reply for a real solution. Arpad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP preg_replace help
Chaim Chaikin wrote: Hello, I am a beginner in PHP. I need help with the function preg_replace. I am trying to remove the backslashes ( \ ) from a string that is submitted by the user. It is submitted in a form but it adds \ before the quotation marks ( ). Will this change if I use the GET method instead of POST. If not can you please tell me how to use preg_replace to remove the backslashes. Don't, use stripslashes() instead. http://us.php.net/stripslashes Here is a nice little hack that I use. plaintext?php print_r($_REQUEST); function stripInput($ar) { $ar = stripslashes($ar); } if ( get_magic_quotes_gpc() ) { array_walk_recursive($_REQUEST, 'stripInput'); array_walk_recursive($_POST,'stripInput'); array_walk_recursive($_GET, 'stripInput'); } print_r($_REQUEST); ? you should see the difference -- 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] PHP preg_replace help
Hello, I am a beginner in PHP. I need help with the function preg_replace. I am trying to remove the backslashes ( \ ) from a string that is submitted by the user. It is submitted in a form but it adds \ before the quotation marks ( ). Will this change if I use the GET method instead of POST. If not can you please tell me how to use preg_replace to remove the backslashes. Thank You, Chaim Chaikin Novice PHP programmer Hotwire Band Website Administrator http://hotwire.totalh.com
Re: [PHP] PHP preg_replace help
Jim Lucas wrote: Here is a nice little hack that I use. Little hack it is, nice it isn't. Ideally just turn off magic_quotes_gpc - you can do so in php.ini, or perhaps your web server configuration files (httpd.conf, .htaccess etc.). If you don't have access to any of the above then install the latest version of PHP_Compat (http://pear.php.net/package/PHP_Compat) and include 'PHP/Compat/Environment/magic_quotes_gpc_off.php'. Reversing the effects of magic_quotes_gpc at runtime is far from trivial, there's lots of potential for subtle bugs, let alone completely forgetting about $_COOKIE. If you're unable to install PHP_Compat, you can grab the relevant files from CVS: http://cvs.php.net/viewvc.cgi/pear/PHP_Compat/Compat/Environment/_magic_quotes_inputs.php?revision=1.3view=markup http://cvs.php.net/viewvc.cgi/pear/PHP_Compat/Compat/Environment/magic_quotes_gpc_off.php?revision=1.7view=markup Arpad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace() help
What am I doing wrong? Using regular expressions when you don't need to: $txt = str_replace(' ', 'nbsp;', substr($txt, strpos($txt, --))); Might be a few typos in there. And I may have mixed up the args. -- Richard Heyes +44 (0)844 801 1072 http://www.websupportsolutions.co.uk Knowledge Base and HelpDesk software that can cut the cost of online support -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_replace() help
I have quotes like the following: $txt = 'A promise is a debt. -- Irish Proverb'; I'd like to replace all the spaces afer the '--' with nbsp; This is what I've tried: $pat = '/( --.*)(\s|\n)/U'; $rpl = '$1$2nbsp;'; while (preg_match($pat,$txt,$matches) 0) { print $txt\n; printf([0]: %s\n,$matches[0]); printf([1]: %s\n,$matches[1]); printf([2]: %s\n,$matches[2]); preg_replace($pat,$rpl,$txt); } The prints are for debugging. $matches contains what I expect but nothing gets replaced and $txt stays the same so it loops forever. What am I doing wrong? -- Everyone is as God has made him, and oftentimes a great deal worse. -- Miguel De Cervantes Rick Pasotto[EMAIL PROTECTED]http://www.niof.net -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace() help
Rick Pasotto wrote: I have quotes like the following: $txt = 'A promise is a debt. -- Irish Proverb'; I'd like to replace all the spaces afer the '--' with nbsp; This is what I've tried: $pat = '/( --.*)(\s|\n)/U'; $rpl = '$1$2nbsp;'; while (preg_match($pat,$txt,$matches) 0) { print $txt\n; printf([0]: %s\n,$matches[0]); printf([1]: %s\n,$matches[1]); printf([2]: %s\n,$matches[2]); preg_replace($pat,$rpl,$txt); } The prints are for debugging. $matches contains what I expect but nothing gets replaced and $txt stays the same so it loops forever. What am I doing wrong? Maybe this ?php $txt = 'A promise is a debt. -- Irish Proverb'; $parts = explode('--', $txt, 2); $parts[1] = str_replace(' ', 'nbsp;', $parts[1]); echo join('--', $parts); ? -- 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
Re: [PHP] preg_replace() help
On 7/13/07, Rick Pasotto [EMAIL PROTECTED] wrote: I have quotes like the following: $txt = 'A promise is a debt. -- Irish Proverb'; I'd like to replace all the spaces afer the '--' with nbsp; This is what I've tried: $pat = '/( --.*)(\s|\n)/U'; $rpl = '$1$2nbsp;'; while (preg_match($pat,$txt,$matches) 0) { print $txt\n; printf([0]: %s\n,$matches[0]); printf([1]: %s\n,$matches[1]); printf([2]: %s\n,$matches[2]); preg_replace($pat,$rpl,$txt); } The prints are for debugging. $matches contains what I expect but nothing gets replaced and $txt stays the same so it loops forever. What am I doing wrong? -- Everyone is as God has made him, and oftentimes a great deal worse. -- Miguel De Cervantes Rick Pasotto[EMAIL PROTECTED]http://www.niof.net -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php If you mean that EVERY space after the -- separator should be replaced, then just try this: ? $txt = As a rule, kids with meningitis don't smile when looking at attractive faces -- not even you, Miss America, or the ravishing pharmacist I mentioned above. -- ER Questions and Answers, Part 3; function replace_space($txt) { $field = explode(--,$txt); for($i=0;$i(count($field)-1);$i++) $new_txt .= $field[$i]; $new_txt .= --.str_replace( ,nbsp;,$field[(count($field)-1)]); return $new_txt; } echo replace_space($txt).\n; ? This would print: A promise is a debt. --nbsp;Irishnbsp;Proverb Conversely, it will still properly handle the existence of double-hyphens anywhere else in the quote, but not the source. Consider this string: As a rule, kids with meningitis don't smile when looking at attractive faces -- not even you, Miss America, or the ravishing pharmacist I mentioned above. -- ER Questions and Answers, Part 3 This would still become: As a rule, kids with meningitis don't smile when looking at attractive faces -- not even you, Miss America, or the ravishing pharmacist I mentioned above. --nbsp;ERnbsp;Questionsnbsp;andnbsp;Answers,nbsp;Partnbsp;3 However, if you have double-hyphens in the source, like so: What a pain in the butt this would be! -- Me, sending an example -- to you The phrase would be printed like this: What a pain in the butt this would be! -- Me, sending an example --nbsp;tonbsp;you -- Daniel P. Brown [office] (570-) 587-7080 Ext. 272 [mobile] (570-) 766-8107 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace() help
Rick Pasotto wrote: I have quotes like the following: $txt = 'A promise is a debt. -- Irish Proverb'; I'd like to replace all the spaces afer the '--' with nbsp; This is what I've tried: $pat = '/( --.*)(\s|\n)/U'; $rpl = '$1$2nbsp;'; while (preg_match($pat,$txt,$matches) 0) { print $txt\n; printf([0]: %s\n,$matches[0]); printf([1]: %s\n,$matches[1]); printf([2]: %s\n,$matches[2]); preg_replace($pat,$rpl,$txt); } The prints are for debugging. $matches contains what I expect but nothing gets replaced and $txt stays the same so it loops forever. What am I doing wrong? What is your goal for this? -- 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
Re: [PHP] preg_replace() help
On Fri, July 13, 2007 3:52 pm, Rick Pasotto wrote: I have quotes like the following: $txt = 'A promise is a debt. -- Irish Proverb'; I'd like to replace all the spaces afer the '--' with nbsp; This is what I've tried: $pat = '/( --.*)(\s|\n)/U'; You might want to use \\s and \\n, so you are 100% clear that the PHP strings have a single \ in them, and that they don't have a newline. The .* is probably messing you up... You could probably manage this with some kind of preg_replace_callback, but it seems to me it would be easier to do: $txt = 'A promise is a debt. -- Irish Proverb'; $pos = strpos($txt, '--'); $html = substr($txt, 0, $pos) . '--' . str_replace(' ', 'nbsp;', substr($txt, $pos + 2)); -- Some people have a gift link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace and regular expressions.
http://php.net/preg_replace_all And be sure to use Ungreedy flag to your pattern: /pattern/U On Sat, April 14, 2007 11:22 pm, Travis Moore wrote: Okay, so what I have is a BB code type of thing for a CMS, which I for obvious reasons can't allow HTML. Here's the snippet of my function: function bbCode($str) { $db = new _Mysql; $strOld = $str; $strNew = $str; $getRegexs = $db-query(SELECT `regex`,`replace`,`search` FROM `_bb_codes`); while ($getRegex = mysql_fetch_assoc($getRegexs)) { $search = base64_decode($getRegex['search']); $regex = base64_decode($getRegex['regex']); $replace = base64_decode($getRegex['replace']); if (preg_match($search,$strNew) == 1) { for ($i = 1; $i 20; $i++) { $strNew = $strOld; $strNew = preg_replace($regex,$replace,$strNew); if ($strNew == $strOld) { break; } else { $strOld = $strNew; } } } } $return = $strNew; return $return; } ** But, for something like this: [quote][quote]Quote #2[/quote]Quote #1[/quote]No quote. I'll get: div class=quoteContainer [quote]Quote #2[/quote]Quote #1/div No quote. Despite being in the loop. Regex is: /\[quote\]((.*|\n)*)\[\/quote\]/ Replace is: div class=messageQuote$1/div Both are stored base64 encoded in a database. Any help / suggestions much appreciated. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Some people have a gift link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] preg_replace and regular expressions.
In your regex, you have a greedy matcher, i.e. .* will match as much as it can to satisfy its condition. I believe you can do .*? and it will work, as .*? will match as little as it can to be satisfied. -Logan -Original Message- From: Travis Moore [mailto:[EMAIL PROTECTED] Sent: Sunday, April 15, 2007 12:22 AM To: [EMAIL PROTECTED] Subject: [PHP] preg_replace and regular expressions. Okay, so what I have is a BB code type of thing for a CMS, which I for obvious reasons can't allow HTML. Here's the snippet of my function: function bbCode($str) { $db = new _Mysql; $strOld = $str; $strNew = $str; $getRegexs = $db-query(SELECT `regex`,`replace`,`search` FROM `_bb_codes`); while ($getRegex = mysql_fetch_assoc($getRegexs)) { $search = base64_decode($getRegex['search']); $regex = base64_decode($getRegex['regex']); $replace = base64_decode($getRegex['replace']); if (preg_match($search,$strNew) == 1) { for ($i = 1; $i 20; $i++) { $strNew = $strOld; $strNew = preg_replace($regex,$replace,$strNew); if ($strNew == $strOld) { break; } else { $strOld = $strNew; } } } } $return = $strNew; return $return; } ** But, for something like this: [quote][quote]Quote #2[/quote]Quote #1[/quote]No quote. I'll get: div class=quoteContainer [quote]Quote #2[/quote]Quote #1/div No quote. Despite being in the loop. Regex is: /\[quote\]((.*|\n)*)\[\/quote\]/ Replace is: div class=messageQuote$1/div Both are stored base64 encoded in a database. Any help / suggestions much appreciated. -- 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
[PHP] preg_replace and regular expressions.
Okay, so what I have is a BB code type of thing for a CMS, which I for obvious reasons can't allow HTML. Here's the snippet of my function: function bbCode($str) { $db = new _Mysql; $strOld = $str; $strNew = $str; $getRegexs = $db-query(SELECT `regex`,`replace`,`search` FROM `_bb_codes`); while ($getRegex = mysql_fetch_assoc($getRegexs)) { $search = base64_decode($getRegex['search']); $regex = base64_decode($getRegex['regex']); $replace = base64_decode($getRegex['replace']); if (preg_match($search,$strNew) == 1) { for ($i = 1; $i 20; $i++) { $strNew = $strOld; $strNew = preg_replace($regex,$replace,$strNew); if ($strNew == $strOld) { break; } else { $strOld = $strNew; } } } } $return = $strNew; return $return; } ** But, for something like this: [quote][quote]Quote #2[/quote]Quote #1[/quote]No quote. I'll get: div class=quoteContainer [quote]Quote #2[/quote]Quote #1/div No quote. Despite being in the loop. Regex is: /\[quote\]((.*|\n)*)\[\/quote\]/ Replace is: div class=messageQuote$1/div Both are stored base64 encoded in a database. Any help / suggestions much appreciated. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_replace();
Hi all, I want replace the | (pipe) and the (space) chars where are between (double-quotes) by an underscore _ with the preg_replace(); funtction. Can someone help me to find the correct regex. Thanks in advance Seb
Re: [PHP] preg_replace();
I am not a very experienced programmer, but I think that str_replace can be used in this case: $new_string=str_replace('|', '_', $old_string) then use the same function to replace spaces. Ed Friday, February 2, 2007, 9:30:37 PM, you wrote: Hi all, I want replace the | (pipe) and the (space) chars where are between (double-quotes) by an underscore _ with the preg_replace(); funtction. Can someone help me to find the correct regex. Thanks in advance Seb -- Best regards, mailto:[EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re[2]: [PHP] preg_replace();
I have tasted the code and it worked fine (if I got you right): $old_string=lazy \|\ dog; $new_string=str_replace('|', '_', $old_string); print $new_string; I got lazy_dog Ed Friday, February 2, 2007, 10:01:14 PM, you wrote: Thanks, but I think that I must use preg_replace because the condition is: replace the chars (pipe or space) when they are between ie : src=file:///h|/hjcjdgh dlkgj/dgjk.jpg to src=file:///h_/hjcjdgh_dlkgj/dgjk.jpg Seb - Original Message - From: [EMAIL PROTECTED] To: Sébastien WENSKE [EMAIL PROTECTED] Cc: php-general@lists.php.net Sent: Friday, February 02, 2007 8:38 PM Subject: Re: [PHP] preg_replace(); I am not a very experienced programmer, but I think that str_replace can be used in this case: $new_string=str_replace('|', '_', $old_string) then use the same function to replace spaces. Ed Friday, February 2, 2007, 9:30:37 PM, you wrote: Hi all, I want replace the | (pipe) and the (space) chars where are between (double-quotes) by an underscore _ with the preg_replace(); funtction. Can someone help me to find the correct regex. Thanks in advance Seb -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re[4]: [PHP] preg_replace();
Try this one: $old_string=lazy \some chars|some chars\ dog; $new_string=str_replace('|', '_', $old_string); print $new_string; Ed Friday, February 2, 2007, 10:39:59 PM, you wrote: ok, but : $old_string=lazy \some chars|some chars\ dog; $new_string=str_replace('|', '_', $old_string); don't work sorry for my bad english, i'm french. - Original Message - From: [EMAIL PROTECTED] To: Sébastien WENSKE [EMAIL PROTECTED] Sent: Friday, February 02, 2007 9:22 PM Subject: Re[2]: [PHP] preg_replace(); I have tasted the code and it worked fine (if I got you right): $old_string=lazy \|\ dog; $new_string=str_replace('|', '_', $old_string); print $new_string; I got lazy_dog Ed Friday, February 2, 2007, 10:01:14 PM, you wrote: Thanks, but I think that I must use preg_replace because the condition is: replace the chars (pipe or space) when they are between ie : src=file:///h|/hjcjdgh dlkgj/dgjk.jpg to src=file:///h_/hjcjdgh_dlkgj/dgjk.jpg Seb - Original Message - From: [EMAIL PROTECTED] To: Sébastien WENSKE [EMAIL PROTECTED] Cc: php-general@lists.php.net Sent: Friday, February 02, 2007 8:38 PM Subject: Re: [PHP] preg_replace(); I am not a very experienced programmer, but I think that str_replace can be used in this case: $new_string=str_replace('|', '_', $old_string) then use the same function to replace spaces. Ed Friday, February 2, 2007, 9:30:37 PM, you wrote: Hi all, I want replace the | (pipe) and the (space) chars where are between (double-quotes) by an underscore _ with the preg_replace(); funtction. Can someone help me to find the correct regex. Thanks in advance Seb -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace();
This always works for me: if (preg_match_all(!\(.+)\!sU, $var, $match)) { for ($i=0; $icount($match[0]); $i++) { $old = $match[1][$i]; $new = preg_replace(!\|| !, _, $old); $var = str_replace(\$old\, \$new\, $var); } } On Fri, Feb 02, 2007 at 07:30:37PM +0100, Sébastien WENSKE wrote: Hi all, I want replace the | (pipe) and the (space) chars where are between (double-quotes) by an underscore _ with the preg_replace(); funtction. Can someone help me to find the correct regex. Thanks in advance Seb -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace(); [solved]
nice ! thanks Steffen Ed ! i've just add '[src|background] *= *' to make sure that the replacement takes effect only in THML tag's attributes if (preg_match_all(![src|background] *= *\(.+)\!sU, $htmlContent, $match)) { for ($i=0; $icount($match[0]); $i++) { $old = $match[1][$i]; $new = preg_replace(!\|| !, _, $old); $htmlContent = str_replace(\$old\, \$new\, $htmlContent); } } - Original Message - From: Steffen Ebermann [EMAIL PROTECTED] To: php-general@lists.php.net Cc: Sébastien WENSKE [EMAIL PROTECTED] Sent: Friday, February 02, 2007 9:01 PM Subject: Re: [PHP] preg_replace(); This always works for me: if (preg_match_all(!\(.+)\!sU, $var, $match)) { for ($i=0; $icount($match[0]); $i++) { $old = $match[1][$i]; $new = preg_replace(!\|| !, _, $old); $var = str_replace(\$old\, \$new\, $var); } } On Fri, Feb 02, 2007 at 07:30:37PM +0100, Sébastien WENSKE wrote: Hi all, I want replace the | (pipe) and the (space) chars where are between (double-quotes) by an underscore _ with the preg_replace(); funtction. Can someone help me to find the correct regex. Thanks in advance Seb -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace(); [solved]
Maybe you just mistyped that, but this would *probably* also match on s= or bar=, cause [ and ] are metacharacters. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace();
On Fri, Feb 02, 2007 at 09:01:38PM +0100, Steffen Ebermann wrote: $new = preg_replace(!\|| !, _, $old); Heyha, the mail's subject gone obsolete. preg_replace isn't necessary at all. Better use: $new = str_replace(array (|, ), _, $old); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace();
On Fri, February 2, 2007 12:30 pm, Sébastien WENSKE wrote: I want replace the | (pipe) and the (space) chars where are between (double-quotes) by an underscore _ with the preg_replace(); funtction. Can someone help me to find the correct regex. You can even go so far so to do both at once: $text = str_replace(array('|', ' '), array('_', '_'), $text); This does ignore the initial requirement of only replacing the ones between quotes, however... Something like this will honor the quotes restriction: $text = preg_replace_all('/(.*)[| ](.*)/, '\\1_\\2', $text); I probably got the PCRE wrong, but it's close. Download The Regex Coach and play with it. :-) -- 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? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace (again)
On Wed, September 20, 2006 11:20 am, Pawel Miroslawski wrote: Hi it's example script: ?php $string = This is some _color:pink_ colored text _color_; $patterns[0] = '/_color:(.*?)_/'; $patterns[1] = '/_color_/'; $replacements[0] = 'font color=$1'; $replacements[1] = '/font'; echo preg_replace($patterns, $replacements, $string); ? It should be ok, but i don't test it. For the sanitization... .*? could be something more like: [#a-z0-9A-Z]+ The # assumes you want to allow #ff style colors. Since this whitelists the specific characters you allow, it's probably better than the strip_tags thing. -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] preg_replace (again) [solved]
On Wed, 2006-09-20 at 11:45 +0700, Peter Lauri wrote: Just to share my solution: Out of curiosity, why don't you go with the very well known BBCode system? preg_replace('/_color:(.*?)_(.*?)_color_/i', 'font color=$1$2/font', $html); Hopefully this is a private system, otherwise someone not very nice might do the following: This is some _color:pink script type=text/javascript language=javascript document.location = 'http://www.myDoityPr0nCollection.com'; /scriptfont color=pink_ colored text _color_ that I want to transfer You need better content sanitization ]:B FWIW, the font tag is about as deprecated as deprecated can get. You might consider switching to span. Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] preg_replace (again) [solved]
Hi, Thanks for you comment. I already changed to span. About sanitation: Do you know any open source where it checks code if it is acceptable or not? Or should I just create a lib that do some preg_match to see if any javascript tag is inside (assuming javascript should not be allowed). This is a private system, so I do not worry so much :) /Peter -Original Message- From: Robert Cummings [mailto:[EMAIL PROTECTED] Sent: Wednesday, September 20, 2006 2:13 PM To: Peter Lauri Cc: 'PHP General' Subject: RE: [PHP] preg_replace (again) [solved] On Wed, 2006-09-20 at 11:45 +0700, Peter Lauri wrote: Just to share my solution: Out of curiosity, why don't you go with the very well known BBCode system? preg_replace('/_color:(.*?)_(.*?)_color_/i', 'font color=$1$2/font', $html); Hopefully this is a private system, otherwise someone not very nice might do the following: This is some _color:pink script type=text/javascript language=javascript document.location = 'http://www.myDoityPr0nCollection.com'; /scriptfont color=pink_ colored text _color_ that I want to transfer You need better content sanitization ]:B FWIW, the font tag is about as deprecated as deprecated can get. You might consider switching to span. Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace (again) [solved]
Well you can use string strip_tags ( string str [, string allowable_tags] ) function Andy Peter Lauri wrote: Hi, Thanks for you comment. I already changed to span. About sanitation: Do you know any open source where it checks code if it is acceptable or not? Or should I just create a lib that do some preg_match to see if any javascript tag is inside (assuming javascript should not be allowed). This is a private system, so I do not worry so much :) /Peter -Original Message- From: Robert Cummings [mailto:[EMAIL PROTECTED] Sent: Wednesday, September 20, 2006 2:13 PM To: Peter Lauri Cc: 'PHP General' Subject: RE: [PHP] preg_replace (again) [solved] On Wed, 2006-09-20 at 11:45 +0700, Peter Lauri wrote: Just to share my solution: Out of curiosity, why don't you go with the very well known BBCode system? preg_replace('/_color:(.*?)_(.*?)_color_/i', 'font color=$1$2/font', $html); Hopefully this is a private system, otherwise someone not very nice might do the following: This is some _color:pink script type=text/javascript language=javascript document.location = 'http://www.myDoityPr0nCollection.com'; /scriptfont color=pink_ colored text _color_ that I want to transfer You need better content sanitization ]:B FWIW, the font tag is about as deprecated as deprecated can get. You might consider switching to span. Cheers, Rob. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace (again)
Hi it's example script: ?php $string = This is some _color:pink_ colored text _color_; $patterns[0] = '/_color:(.*?)_/'; $patterns[1] = '/_color_/'; $replacements[0] = 'font color=$1'; $replacements[1] = '/font'; echo preg_replace($patterns, $replacements, $string); ? It should be ok, but i don't test it. Pawel
[PHP] preg_replace (again)
Hi group, I know I am a little bit stupid when it comes to actually figuring out how to use the preg_match and preg_replace. This is what I am facing: A string like this: This is some _color:pink_ colored text _color_ that I want to transfer Should convert to: This is some font color=pinkcolored text/font that I want to transfer Anyone who see a simple solution to this? Right now I have created an ugly script that do the same thing, but I want to start to learn and use preg_match. Thanks. /Peter www.lauri.se http://www.lauri.se/ - Personal web site www.dwsasia.com http://www.dwsasia.com/ - Company web site
RE: [PHP] preg_replace (again) [solved]
Just to share my solution: preg_replace('/_color:(.*?)_(.*?)_color_/i', 'font color=$1$2/font', $html); /Peter -Original Message- From: Peter Lauri [mailto:[EMAIL PROTECTED] Sent: Wednesday, September 20, 2006 9:42 AM To: 'PHP General' Subject: [PHP] preg_replace (again) Hi group, I know I am a little bit stupid when it comes to actually figuring out how to use the preg_match and preg_replace. This is what I am facing: A string like this: This is some _color:pink_ colored text _color_ that I want to transfer Should convert to: This is some font color=pinkcolored text/font that I want to transfer Anyone who see a simple solution to this? Right now I have created an ugly script that do the same thing, but I want to start to learn and use preg_match. Thanks. /Peter www.lauri.se http://www.lauri.se/ - Personal web site www.dwsasia.com http://www.dwsasia.com/ - Company web site -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace \\1 yIKES!
On Jun 13, 2006, at 1:58 PM, tedd wrote: At 11:33 AM -0700 6/13/06, sam wrote: Wow this is hard I can't wait till I get the hang of it. Capitalize the first letter of a word. Try: ?php $word = yikes; $word[0]=strtoupper($word[0]); echo($word); ? This blows my mind. What should one think, everything is an array? Well, okay not every but everything that's in linear consecutive order; anything that can be indexed? I was trying to make an array from $word but explode() doesn't take an empty delimiter so I gave up and went for the preg_replace. And hey yo, Jochem, I did RTFM, for hours, I always do before I post to the list. I just missed all the answers in the fine manual this time. Cut me some slack. Where should I wire the Euros to? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace \\1 yIKES!
sam wrote: On Jun 13, 2006, at 1:58 PM, tedd wrote: At 11:33 AM -0700 6/13/06, sam wrote: Wow this is hard I can't wait till I get the hang of it. Capitalize the first letter of a word. Try: ?php $word = yikes; $word[0]=strtoupper($word[0]); echo($word); ? This blows my mind. What should one think, everything is an array? an array is an array. a string is a string. characters in a string can be accessed using array-like notation using the offset postion of the relevant char (elements are zero based) Well, okay not every but everything that's in linear consecutive order; anything that can be indexed? I was trying to make an array from $word but explode() doesn't take an empty delimiter so I gave up and went for the preg_replace. And hey yo, Jochem, I did RTFM, for hours, I always do before I post to the list. I just I'd tell you to RTFM (although I did tell you to read the manual regarding the specifics of using preg_replace()'s 'e' modifier after showing you a working example of how to use it, based on your original code snippet) I did berate the fact that you waited no more than 7 minutes before sending a 'help me' reminder regarding your original post. missed all the answers in the fine manual this time. Cut me some slack. cut you slack? are you a graphic designer or something? Where should I wire the Euros to? my paypal account name is my email address :-) --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: [PHP] preg_replace \\1 yIKES!
Jochem Maas wrote: I did berate the fact that you waited no more than 7 minutes before sending a 'help me' reminder regarding your original post. While I agree with most of what you are saying, you may want to check that email again. Sams 'for Eyes burning...' email was in response to someone privately suggesing that he use ucfirst. It was *not* a 'help me' reminder, so drop that criticism please. Aside from that, anyone who manages to miss ucfirst when R'ingTFM for this problem should RTFM some more. -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace \\1 yIKES!
And hey yo, Jochem, I did RTFM, for hours, I always do before I post to the list. I just I'd tell you to RTFM (although I did tell you to read the manual regarding the specifics of using preg_replace()'s 'e' modifier after showing you a working example of how to use it, based on your original code snippet) Yes, I'm using your example right now to help me learn this preg_replace beast. The php.net page on preg_replace was just to overwhelming for the exhausted state of mind I was in. I did berate the fact that you waited no more than 7 minutes before sending a 'help me' reminder regarding your original post. No, that was a misunderstanding, I was just saying thanks to the guy who said, why not just use ulfirst(). (Which I also missed on the php.net page on strings.) missed all the answers in the fine manual this time. Cut me some slack. cut you slack? are you a graphic designer or something? How did you know? I was 'in' graphics for 30 years before I threw my hands up at that lunatic world and decided to become a programmer. I love the printing industry but ever since the Mac took over everybody went nuts. The printers are still mad, after 15 years, that the Mac took away their razor blades and rubylith. Checks in the mail. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace \\1 yIKES!
Stut wrote: Jochem Maas wrote: I did berate the fact that you waited no more than 7 minutes before sending a 'help me' reminder regarding your original post. While I agree with most of what you are saying, you may want to check that email again. Sams 'for Eyes burning...' email was in response to someone privately suggesing that he use ucfirst. It was *not* a 'help me' reminder, so drop that criticism please. consider it dropped - I didn't catch that it was a reply to an offlist post. Aside from that, anyone who manages to miss ucfirst when R'ingTFM for this problem should RTFM some more. -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace \\1 yIKES!
At 3:45 AM -0700 6/14/06, sam wrote: On Jun 13, 2006, at 1:58 PM, tedd wrote: At 11:33 AM -0700 6/13/06, sam wrote: Wow this is hard I can't wait till I get the hang of it. Capitalize the first letter of a word. Try: ?php $word = yikes; $word[0]=strtoupper($word[0]); echo($word); ? This blows my mind. What should one think, everything is an array? Well, okay not every but everything that's in linear consecutive order; anything that can be indexed? I was trying to make an array from $word but explode() doesn't take an empty delimiter so I gave up and went for the preg_replace. Sorry, it was a throwback to my C days -- using ucfist() is better. tedd -- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_replace \\1 yIKES!
Wow this is hard I can't wait till I get the hang of it. Capitalize the first letter of a word. echo preg_replace('/(^)(.)(.*$)/', strtoupper('\\2') . '\\3', 'yikes!'); // outputs yikes! Nope didn't work. So I want to see if I'm in the right place: echo preg_replace('/(^)(.)(.*$)/', '\\1' . '-' . strtoupper('\\2') . '-' . '\\3', 'yikes!'); //outputs -y-ikes! So yea I've got it surrounded why now strtoupper: Yikes! Thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace \\1 yIKES!
for Eyes burning; caffein shakes; project overdue Thanks Why not just use ucfirst http://us2.php.net/manual/en/ function.ucfirst.php? -Original Message- From: sam [mailto:[EMAIL PROTECTED] Sent: Tuesday, June 13, 2006 2:34 PM To: PHP Subject: [PHP] preg_replace \\1 yIKES! Wow this is hard I can't wait till I get the hang of it. Capitalize the first letter of a word. echo preg_replace('/(^)(.)(.*$)/', strtoupper('\\2') . '\\3', 'yikes!'); // outputs yikes! Nope didn't work. So I want to see if I'm in the right place: echo preg_replace('/(^)(.)(.*$)/', '\\1' . '-' . strtoupper('\\2') . '-' . '\\3', 'yikes!'); //outputs -y-ikes! So yea I've got it surrounded why now strtoupper: Yikes! Thanks -- 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: [PHP] preg_replace \\1 yIKES!
sam wrote: Wow this is hard I can't wait till I get the hang of it. Capitalize the first letter of a word. echo preg_replace('/(^)(.)(.*$)/', strtoupper('\\2') . '\\3', 'yikes!'); // outputs yikes! Nope didn't work. So I want to see if I'm in the right place: echo preg_replace('/(^)(.)(.*$)/', '\\1' . '-' . strtoupper('\\2') . '-' . '\\3', 'yikes!'); //outputs -y-ikes! So yea I've got it surrounded why now strtoupper: Yikes! to running strtoupper() on the string literal '\\2' which after it has been capitalized will be '\\2'. if you *really* want to use preg_replace(): echo preg_replace(/(^)(.)(.*\$)/e, \\\1\ . strtoupper(\\\2\) . \\\3\, yikes!), \n; notice the 'e' modifier on the regexp - it causes the replace string to be treated as a string of php code that is autoamtically evaluated - checvk the manual for more info. (I used double quotes only so that I could test the code on the cmdline using 'php -r') ...BUT I suggest you try this function instead: ucfirst() Thanks --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: [PHP] preg_replace \\1 yIKES!
sam wrote: for Eyes burning; caffein shakes; project overdue nobody here cares whether your project is overdue - waiting 7 minutes before sending a 'reminder' about the question you asked suggests you need to take a PATIENCE lesson. or did some fraudster sell you a php support contract? ... for 500 euros you can contact me anytime at [EMAIL PROTECTED] for all you php woes :-P Thanks Why not just use ucfirst http://us2.php.net/manual/en/function.ucfirst.php? -Original Message- From: sam [mailto:[EMAIL PROTECTED] Sent: Tuesday, June 13, 2006 2:34 PM To: PHP Subject: [PHP] preg_replace \\1 yIKES! Wow this is hard I can't wait till I get the hang of it. Capitalize the first letter of a word. echo preg_replace('/(^)(.)(.*$)/', strtoupper('\\2') . '\\3', 'yikes!'); // outputs yikes! Nope didn't work. So I want to see if I'm in the right place: echo preg_replace('/(^)(.)(.*$)/', '\\1' . '-' . strtoupper('\\2') . '-' . '\\3', 'yikes!'); //outputs -y-ikes! So yea I've got it surrounded why now strtoupper: Yikes! Thanks --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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace \\1 yIKES!
On Tue, 2006-06-13 at 15:07, Jochem Maas wrote: sam wrote: for Eyes burning; caffein shakes; project overdue nobody here cares whether your project is overdue - waiting 7 minutes before sending a 'reminder' about the question you asked suggests you need to take a PATIENCE lesson. or did some fraudster sell you a php support contract? ... for 500 euros you can contact me anytime at [EMAIL PROTECTED] for all you php woes :-P Oooh, GREAT IDEA!! *scribbles down into his notebook* :B Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace \\1 yIKES!
At 11:33 AM -0700 6/13/06, sam wrote: Wow this is hard I can't wait till I get the hang of it. Capitalize the first letter of a word. Try: ?php $word = yikes; $word[0]=strtoupper($word[0]); echo($word); ? tedd -- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace \\1 yIKES!
On 13/06/06, tedd [EMAIL PROTECTED] wrote: At 11:33 AM -0700 6/13/06, sam wrote: Wow this is hard I can't wait till I get the hang of it. Capitalize the first letter of a word. Why not use ucfirst(), that is what the function is for. -- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- http://www.web-buddha.co.uk http://www.projectkarma.co.uk
Re: [PHP] preg_replace learning resources? Regex tuts? Tips? (and yes, I have been rtfm)
This one time, at band camp, Micky Hulse [EMAIL PROTECTED] wrote: Hi all, I have been rtfm on preg_replace, and I am a bit turned-off by how complex reg-exing appears to be anyway, I would like to spend some time learning how I would convert a file full of links that look like: Try this quicky http://phpro.org/tutorials/Introduction-to-PHP-Regular-Expressions.html Kevin -- Democracy is two wolves and a lamb voting on what to have for lunch. Liberty is a well-armed lamb contesting the vote. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php