[PHP] Re: preg_replace question
On 12-12-2012 17:11, Curtis Maurand wrote: 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 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. - Tul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: preg_replace question
Maciek Sokolewicz maciek.sokolew...@gmail.com wrote: On 12-12-2012 17:11, Curtis Maurand wrote: 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 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. - Tul Sometimes if all you know is preg_replace(), everything looks like a nail... -- Sent from my Android phone with K-9 Mail. Please excuse my brevity. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: preg_replace question
On 12/12/2012 12:00 PM, Maciek Sokolewicz wrote: On 12-12-2012 17:11, Curtis Maurand wrote: 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 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: preg_replace question
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: 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 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? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: preg_replace question
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: 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 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: preg_replace insert newline
On 06/02/2010 04:28 PM, Sam Smith wrote: $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 http://us.php.net/manual/en/language.types.string.php Pay attention to single and double. -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: preg_replace anything that isn't WORD
דניאל דנון 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? Capture everything but only replace the backreference for the words: $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] Re: preg_replace anything that isn't WORD
Didn't seem to make it the first time. Shawn McKenzie wrote: דניאל דנון 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? Capture everything but only replace the backreference for the words: $r = preg_replace('#.*?(cat|dog).*?#', '\1', $s); -Shawn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: preg_replace problem
Hi. On Saturday 13 June 2009, Al wrote: I may not have been very clear. Feed it just test with the quotes. You should get back, test the same as you gave it. Instead, I get back quote;testquote; Like Shawn, I have tried your code in isolation and only get the expected results. I looped it over a few test values: ?php $valueList = array('','test','testtest','testamp;test'); echo pre\n\n; foreach ($valueList as $value) { echo Before: $value\n; $value=preg_replace(%(?!amp;)%i, amp;, $value); echo After: $value\n\n; } echo '/pre'; ? outputs (ignoring pre as that was just to remove the br / for easy reading): Before: After: Before: test After: test Before: testtest After: testamp;test Before: testamp;test After: testamp;test The only conclusion is that something else in your code is converting the quotes. Have a look around. HTH Mark -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: preg_replace problem
On Sat, Jun 13, 2009 at 5:16 PM, Aln...@ridersite.org wrote: Al wrote: 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... I erred when I keyed this message. The But, should be as, without the e on quote. Which is an HTML entity for quote. But; if $value is simple a quote character [] I get quot. e.g., test = quot;testquot; The regex that you posted won't replace an actual quote character at all. Are you sure you aren't running the value through something like htmlspecialchars() before it's getting into your regexp? Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: preg_replace problem
Al wrote: 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... Your code works for me, unless I'm misunderstanding the problem. With the following: $value = quote;testquote;; I get: quote;testquote;br / amp;quote;testamp;quote; -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: preg_replace problem
Shawn McKenzie wrote: Al wrote: 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... Your code works for me, unless I'm misunderstanding the problem. With the following: $value = quote;testquote;; I get: quote;testquote;br / amp;quote;testamp;quote; I may not have been very clear. Feed it just test with the quotes. You should get back, test the same as you gave it. Instead, I get back quote;testquote; Like wise, if I give it just a single quote [] I get back [quote;] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: preg_replace problem
Shawn McKenzie wrote: Al wrote: 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... Your code works for me, unless I'm misunderstanding the problem. With the following: $value = quote;testquote;; I get: quote;testquote;br / amp;quote;testamp;quote; I tried IE8 thinking it could be a weird browser bug. Same error. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: preg_replace problem
Al wrote: 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... I erred when I keyed this message. The But, should be as, without the e on quote. Which is an HTML entity for quote. But; if $value is simple a quote character [] I get quot. e.g., test = quot;testquot; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: preg_replace() help
What do you want out? $txt = 'A promise is a debt. -- Irish Proverb'; = [1] $txt = 'A promise is a debt. --Irish Proverb'; OR [2] $txt = 'A promise is a debt. --IrishProverb'; for [1] $txt= preg_replace(%--\x20+%, '--', $txt); //The \x20+ is one or more spaces 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? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: preg_replace \\1 yIKES!
Capitalize the first letter of a word: ucwords()[1] Now, if it's just for practice, then you need a little change in you code, since strtoupper('\\2') will give '\\2' (i.e. does nothing) and the letter will remain the same. One way to do it would be $text = 'hello world (any ...world)'; echo preg_replace('/\b(\w)/Xe', 'strtoupper(\\1)', $text); check the manual[2] for more info... [1] http://php.net/ucwords [2] http://php.net/manual/en/reference.pcre.pattern.syntax.php http://php.net/manual/en/reference.pcre.pattern.modifiers.php 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! -- Atentamente / Sincerely, J. Rafael Salazar Magaña -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: preg_replace problem (or possibly bug)
What I see in the page you sent is a [url] bbcode-tag that is not closed (i.e. it has no [/url] or it's not displayed) If that's not the problem, could you send some example that isn't working (and some others that are working) Michael wrote: I am currently writing a forum system, but at the moment I have a bug that no one can seem to get to the root cause of. Basically I am using preg_replace with the pattern as '\[url=(.*?)\](.*?)\[/url\]'is. However for most links it works fine but for others it just doesn't render the bbcode to a link, we were trying to get to the root cause of it here http://michael-m.co.uk/forums/index.php?action=view_topicid=31page=1 but we failed. I'm not really that good with regular expresions so that might explain why. Also we had never noticed these problems until about 3 days ago, but I see no reason why it could have started. All help will be greatly appreciated, you may use the username php and the password php to log on to do your own tests but please keep the testing to the topic (and the spam forum if necessary). Many Thanks, Michael Mulqueen michael-m.co.uk -- Atentamente, J. Rafael Salazar Magaña Innox - Innovación Inteligente Tel: +52 (33) 3615 5348 ext. 205 / 01 800 2-SOFTWARE http://www.innox.com.mx -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: preg_replace problem
Is there any special reason why you want to solve this with regular expressions? As far as I see, there's a couple of problems with your code, a) $file = dog.txt should be $file = 'dog.txt', b) $getOldValue has the _array_ resulting of parsing the INI file, hence you should write $getOldValue, since it's the complete file (I won't say anything about performance), c) you're using a regular expresion to increment the value in a specific key/index, and you don't need regexp for that, and d) you're not saving the whole file, only the setting (entry) for today, hence you overwrite the file and leave only today = n What do you think of something like this (a bit changed): $file= 'dog.txt'; $today = date(Ymd); // we read the file, $getOldValue = parse_ini_file($file); // set or increment the value in $today key, @$getOldValue[$today] ++; // and write back the whole INI file // note: what we have is an array, not a string $str_ini = ''; foreach ( $getOldValue as $date = $value ) { $str_ini .= $date = $value\n; } file_put_contents($file, $str_ini); Benjamin Adams wrote: $file = dog.txt; $today = date(Ymd); function incDate($new, $date){ //$date = settype('int'); return $new.($date++); } $getOldValue = parse_ini_file($file, 1); $newValue = $getOldValue[$today] + 1; $oldDate = $today . = . $newValue; $newDate = preg_replace('/(\d+\s\=\s)(\d+)/ie', 'incDate($1,$2)', $oldDate); file_put_contents($file, $newDate . \n); This works with one file but with multi lines I'm having trouble: if the file has: 20060301 = 34 20060302 = 3 the file after script will be: 20060302 = 4 I want it to preserve the previous lines so output should be: 20060301 = 34 20060302 = 4 Help would be great, thanks Ben -- Atentamente, J. Rafael Salazar Magaña Innox - Innovación Inteligente Tel: +52 (33) 3615 5348 ext. 205 / 01 800 2-SOFTWARE http://www.innox.com.mx -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: preg_replace - I don't have a clue
Is this the wrong forum for this question - or does no-one else have a clue about this either? At 09:43 AM 1/12/06, Frank Bax wrote: As I understand the docs for preg_replace(), I can enclose an PCRE expression in parenthesis and use a backreference in the replace string; but it's not working! Data coming from another system contains a lot of data in one text record which I must parse. Individual elements in the record are separated by $. I want to fix elements that contain dollar-alpha-space-space-digits-dollar and replace the second space (between word and number) with a plus character to end up with dollar-alpha-space-plus-digits-dollar An example of input string: $prop = '$Fencing 11$Lumber 17$Weight: 317 Stones$Energy Resist 2%$'; Notice that Fencing and Lumber have two spaces. I want to end up with: $prop = '$Fencing +11$Lumber +17$Weight: 317 Stones$Energy Resist 2%$'; Notice that original and final strings are the same length. preg_replace( ' (\d+\$)', '+$1', $prop ); /* results in dollar-alpha-space-space-plus, why have digits-dollar have disappeared? */ $Fencing +Lumber +Weight: 317 Stones$Energy Resist 2%$ Although the docs say that $0 should backreference the whole pattern, instead it seems to match the pattern in parenthesis, but this code : preg_replace( ' (\d+\$)', '+$0', $prop ); /* results in dollar-alpha-space-space-plus-digits-dollar */ $Fencing +11$Lumber +17$Weight: 317 Stones$Energy Resist 2%$ Still contains the two blanks!! In neither of my test cases is the result string the same length as original. I'm running PHP 4.4.0 on OpenBSD 3.7 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] RE: preg_replace to delete all js code in string help needed
Thanks Greg, That sorted that out nicely :-) Dave Carrera -Original Message- From: greg [mailto:[EMAIL PROTECTED] Sent: 09 May 2004 14:17 To: Dave Carrera Subject: Re: preg_replace to delete all js code in string help needed Dave Carrera wrote: $text2 = preg_replace(/script[^]+.*?\/script/is,,$text2); Leaves output with language=javascript blah blah Dose anyone know of a pattern that will get rid of ALL javascript from a string. Thank you for any help Dave Carrera --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.679 / Virus Database: 441 - Release Date: 07/05/2004 Try $text2 = preg_replace(/script[^]*.*\/script/is,,$text2) It works for me. Greg --- Incoming mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.679 / Virus Database: 441 - Release Date: 07/05/2004 --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.679 / Virus Database: 441 - Release Date: 07/05/2004 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: preg_replace with /e modifier
Thanks John! Sorry to pester you any more, but how can I match all Ascii characters? I can't seem to find it on 'http://www.php.net/manual/en/pcre.pattern.syntax.php' (The period is matching Ascii characters and giving me this) Unexpected character in input: ' ' (ASCII=23) state=2 Also how many ascii characters are there? Would it be feasible to add them into the array? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: preg_replace question
[EMAIL PROTECTED] (Electroteque) wrote in news:[EMAIL PROTECTED]: yet another regex question how could i hange the value within the quotes with preg_replace php_value upload_max_filesize 5M $str = preg_replace(#php_value upload_max_filesize\s?['\](.+?)[\']#i, php_value upload_max_filesize\\\1\, $str); Should work. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: preg_replace and eval
The following line sort of works: $retVal = preg_replace(/(.*){parent.(.*)}(.*)/e,'\\1'.\$this-parentNode- getProper tyValue('\\2').'\\3',$retVal); However, if there are more than one string to replace it only works on the last one. So: caption = the parent's coordinates are ({parent.left}, {parent.top}) would only return something like: the parent's coordinates are ({parent.left}, 100) That is because the first (.*) skips the first parent.x pattern. It seems the string is parsed backwards. To avoid this problem, be more specific in your regexp. Don't use (.*), but ([^{]*) instead. The regexp which works in your example is the following: /([^{]*){parent.([^{]*)}([^{]*)/e HTH Erwin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: preg_replace
In article [EMAIL PROTECTED], [EMAIL PROTECTED] (Gerard Samuel) wrote: When you think you got it, you don't get it.. Im trying to scan the link for =A-Z0-9_=, dump the '=' and evaluate the remainder as a defined constant. Yes its funny looking, but if I could get this going Im golden. $bar is always returned with '=_L_OL_HERE=' as the link and not 'here' as it supposed to be. Any help would be appreciated. Thanks ?php define('_L_OL_HERE', 'here'); $foo = 'a href=there.com=_L_OL_HERE=/a'; $bar = preg_replace('/(=)([A-Z0-9_])(=)/e', ' . constant($2) . ', $foo); echo $bar; ? You're trying to match one or more characters in the class, so add a +. And you're not concatenating the expression 'constant($2)' with anything, so lose the extra periods. Parentheses aren't needed around the equals signs, so you might as well drop those too. Ex: preg_replace('/=([A-Z0-9_]+)=/e', 'constant($1)', $foo); -- CC -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: preg_replace(/^\//.. doesnt work?
$a = /test/; $a = preg_replace(/^\//, , $a); echo $a; Notice the difference in the second line... On Wed, 20 Feb 2002, John Ericson wrote: Im having a weird regexp problem in PHP that I think is correct but it doesnt appear to work. The code is this: $a = /test/; preg_replace(/^\//, , $a); echo $a; I want preg_replace to replace the first '/' in $a with '' so that the echo function echoes test/, but instead it echoes /test/ as if preg_replace didnt worked? What am I doing wrong? Im using: PHP Version 4.1.1 Configure Command: './configure' '--with-mysql=/usr/local/mysql' '--with-apxs=/usr/local/apache/bin/apxs' '--with-db2--disable-debug' '--disable-static' '--enable-sockets' '--with-yp' '--with-zlib' Regex Library: Bundled library enabled PCRE (Perl Compatible Regular Expressions) Support: enabled PCRE Library Version: 3.4 22-Aug-2000 Please CC me since Im not a member of this maillinglist. -- * John Ericson [EMAIL PROTECTED] * ICQ: 7325429 JID: [EMAIL PROTECTED] * web: http://john.pp.se -- 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] Re: preg_replace(/^\//.. doesnt work?
thanks! On Feb 20 11:55, Philip Hallstrom wrote: $a = /test/; $a = preg_replace(/^\//, , $a); echo $a; Notice the difference in the second line... On Wed, 20 Feb 2002, John Ericson wrote: Im having a weird regexp problem in PHP that I think is correct but it doesnt appear to work. The code is this: $a = /test/; preg_replace(/^\//, , $a); echo $a; I want preg_replace to replace the first '/' in $a with '' so that the echo function echoes test/, but instead it echoes /test/ as if preg_replace didnt worked? What am I doing wrong? Im using: PHP Version 4.1.1 Configure Command: './configure' '--with-mysql=/usr/local/mysql' '--with-apxs=/usr/local/apache/bin/apxs' '--with-db2--disable-debug' '--disable-static' '--enable-sockets' '--with-yp' '--with-zlib' Regex Library: Bundled library enabled PCRE (Perl Compatible Regular Expressions) Support: enabled PCRE Library Version: 3.4 22-Aug-2000 Please CC me since Im not a member of this maillinglist. -- * John Ericson [EMAIL PROTECTED] * ICQ: 7325429 JID: [EMAIL PROTECTED] * web: http://john.pp.se -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- * John Ericson [EMAIL PROTECTED] * ICQ: 7325429 JID: [EMAIL PROTECTED] * web: http://john.pp.se -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Re: preg_replace(/^\//.. doesnt work?
Also note: ^ matches the beginning of the string, so ^\/ will only match a / at the beginning of the string (not the first occurence of /) -Original Message- From: Philip Hallstrom [mailto:[EMAIL PROTECTED]] Subject: [PHP] Re: preg_replace(/^\//.. doesnt work? $a = /test/; $a = preg_replace(/^\//, , $a); echo $a; On Wed, 20 Feb 2002, John Ericson wrote: I want preg_replace to replace the first '/' in $a with '' so that the echo function echoes test/, but instead it echoes /test/ as if preg_replace didnt worked? What am I doing wrong? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: preg_replace(/^\//.. doesnt work?
Err, that's what he wanted On Wednesday 20 February 2002 12:13 pm, you wrote: Also note: ^ matches the beginning of the string, so ^\/ will only match a / at the beginning of the string (not the first occurence of /) -Original Message- From: Philip Hallstrom [mailto:[EMAIL PROTECTED]] Subject: [PHP] Re: preg_replace(/^\//.. doesnt work? $a = /test/; $a = preg_replace(/^\//, , $a); echo $a; On Wed, 20 Feb 2002, John Ericson wrote: I want preg_replace to replace the first '/' in $a with '' so that the echo function echoes test/, but instead it echoes /test/ as if preg_replace didnt worked? What am I doing wrong? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Re: preg_replace(/^\//.. doesnt work?
that's probably what he meant... but what he asked for was how to replace the first '/' in $a if you have $a=abc/def, the regex ^/ will not replace the first / in your string, which was my point :-) -Original Message- From: James Taylor [mailto:[EMAIL PROTECTED]] Subject: Re: [PHP] Re: preg_replace(/^\//.. doesnt work? Err, that's what he wanted On Wednesday 20 February 2002 12:13 pm, you wrote: Also note: ^ matches the beginning of the string, so ^\/ will only match a / at the beginning of the string (not the first occurence of /) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: preg_replace() 'space' the final frontier
In article 000801c1a9c5$26007460$017f@localhost, [EMAIL PROTECTED] (Hugh Danaher) wrote: What I am trying to do is have a line of text break at a space after reading 19 words. Having read the various methods of finding and replacing one character with another, I settled on preg replace as my best choice, but this function doesn't accept a space in the regular expression slot. What can I do to get around this, or is there a better function than the one I selected? $statement=preg replace( ,br,$original,19); Warning: Empty regular expression in /home/www/host/document.php on line 71 It's not the space character that's prompting the error, it's the absence of regex delimiters around the space. $statement=preg replace(/ /,br,$original,19); http://www.php.net/manual/en/ref.pcre.php http://www.php.net/manual/en/pcre.pattern.syntax.php -- CC -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Re: preg_replace() 'space' the final frontier
PHP gave us strpos() and strrpos(), which find the first and last occurrance of something within a string. What they forgot was rather important: finding the nth occurrance. I wrote some code you can add to your script (or put it in a separate file and require() it) that provides just such a function: function strnpos($string, $search, $nth) { $count = 0; for ($i = 0; $i strlen($string); $i++) { if ($string[$i] == $search) { $count++; if ($count == $nth) { return $i; } } } if ($count != $nth) { return FALSE; } } Remember, PHP was created in C, and C strings are just arrays of characters. That functionality partially carries over to PHP (I say partially because sizeof($string) will return 1, not the length of the string). You can access individual characters just as you would access an individual element of an array. That's what the above code does. The function returns the LOCATION of the nth occurrance, it doesn't do the replacing for you. You can use it with substr_replace() like so: $string = substr_replace($string, br, strnpos($string, , 19), 1); or in a less compact way: $offset = strnpos($string, , 19); $string = substr_replace($string, br, $offset, 1); Hope that helps. Mike Frazer Hugh Danaher [EMAIL PROTECTED] wrote in message 000801c1a9c5$26007460$017f@localhost">news:000801c1a9c5$26007460$017f@localhost... What I am trying to do is have a line of text break at a space after reading 19 words. Having read the various methods of finding and replacing one character with another, I settled on preg_replace as my best choice, but this function doesn't accept a space in the regular expression slot. What can I do to get around this, or is there a better function than the one I selected? $statement=preg_replace( ,br,$original,19); Warning: Empty regular expression in /home/www/host/document.php on line 71 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Re: preg_replace() 'space' the final frontier
NOTE: That was done very quickly and only works on single character searches! I'm working on one that will find multi-character strings. Gimme a few mins and email me off-list if you want it. Mike Mike Frazer [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... PHP gave us strpos() and strrpos(), which find the first and last occurrance of something within a string. What they forgot was rather important: finding the nth occurrance. I wrote some code you can add to your script (or put it in a separate file and require() it) that provides just such a function: function strnpos($string, $search, $nth) { $count = 0; for ($i = 0; $i strlen($string); $i++) { if ($string[$i] == $search) { $count++; if ($count == $nth) { return $i; } } } if ($count != $nth) { return FALSE; } } Remember, PHP was created in C, and C strings are just arrays of characters. That functionality partially carries over to PHP (I say partially because sizeof($string) will return 1, not the length of the string). You can access individual characters just as you would access an individual element of an array. That's what the above code does. The function returns the LOCATION of the nth occurrance, it doesn't do the replacing for you. You can use it with substr_replace() like so: $string = substr_replace($string, br, strnpos($string, , 19), 1); or in a less compact way: $offset = strnpos($string, , 19); $string = substr_replace($string, br, $offset, 1); Hope that helps. Mike Frazer Hugh Danaher [EMAIL PROTECTED] wrote in message 000801c1a9c5$26007460$017f@localhost">news:000801c1a9c5$26007460$017f@localhost... What I am trying to do is have a line of text break at a space after reading 19 words. Having read the various methods of finding and replacing one character with another, I settled on preg_replace as my best choice, but this function doesn't accept a space in the regular expression slot. What can I do to get around this, or is there a better function than the one I selected? $statement=preg_replace( ,br,$original,19); Warning: Empty regular expression in /home/www/host/document.php on line 71 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Re: preg_replace() 'space' the final frontier
Okay that was quicker than I thought. Here's the code to find the nth occurrance of a string within a string: function strnpos($string, $search, $nth) { $count = 0; $len = strlen($string); $slen = strlen($search); for ($i = 0; $i $len; $i++) { if (($i + $slen) $len) { return FALSE; } if (substr($string, $i, $slen) == $search) { $count++; if ($count == $nth) { return $i; } } } if ($count != $nth) { return FALSE; } } It returns the STARTING POINT of the nth occurrance of the string. If you are looking for the first occurrance of the word test and test covers positions 10-13, the function returns 10, just like the built-in functions strpos() and strrpos(). $string is the string to be searched; $search is what you are searcing for; $nth is the number of the occurrance you are looking for. Hope you all can make use of this! Mike Frazer -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Re: preg_replace() 'space' the final frontier
Mike, Thanks for your input on this. I'm getting better at php, but it does take time. Thanks again, Hugh - Original Message - From: Mike Frazer [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Wednesday, January 30, 2002 4:53 PM Subject: [PHP] Re: preg_replace() 'space' the final frontier Okay that was quicker than I thought. Here's the code to find the nth occurrance of a string within a string: function strnpos($string, $search, $nth) { $count = 0; $len = strlen($string); $slen = strlen($search); for ($i = 0; $i $len; $i++) { if (($i + $slen) $len) { return FALSE; } if (substr($string, $i, $slen) == $search) { $count++; if ($count == $nth) { return $i; } } } if ($count != $nth) { return FALSE; } } It returns the STARTING POINT of the nth occurrance of the string. If you are looking for the first occurrance of the word test and test covers positions 10-13, the function returns 10, just like the built-in functions strpos() and strrpos(). $string is the string to be searched; $search is what you are searcing for; $nth is the number of the occurrance you are looking for. Hope you all can make use of this! Mike Frazer -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Re: preg_replace help
First of all, thanks to everyone who replied! I have several good suggestions. Here is one that seems to work pretty well, to cover a multitude of situations: $msg = eregi_replace(([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/=]),a href=\\\1://\\2\\3\ target=\_blank\\\1://\\2\\3/a, $msg); The only problem is that if a correctly formatted link is already in the text, then this routine tries to add an additional href and it gets all messed up. Is there any way that I can use this algorithm and logically tell it not to do the replace if the anchor tags are already in place? I can't just search for an anchor a tag because there can be several in the text. Example: $myLink = a href=\http://www.mytest.com\; target=\_n\Testing/a; echo makeLink($myLink); Creates $msg = a href=a href=http://www.mytest.com; target=_blankhttp://www.mytest.com/a target=_nTesting/a Any suggestions? Gaylen Fraley [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... I have need to be able to replace text within a string to turn it into a link. In other words, I might have a phrase lik: blah, blah, blah, please visit my site at www.mysite.com. blah, blah,blah. I need to have the string converted to blah, blah, blah, please visit my site at http://www.mysite.com. blah, blah,blah. The link might have more than 3 sections and won't necessarily end in .com (.net,.com.nl, etc.). Here is what I have been testing (then I get stuck): $myLink = Please visit my web site at www.mysite.com. You will be glad you did!; $repLink = preg_replace(/(www.)/i,a href=http://\\1,$myLink); which yields Please visit my web site at a href=http://www.mysite.com. You will be glad you did! What I would need is Please visit my web site at a href=http://www.mysite.comwww.mysite.com/a. You will be glad you did! To be truthful, pattern matching is giving me migraines! Programming has always been natural to me (32 years of it), but this challenge (eregi, preg_match,etc.) is making me crazy! Can someone recommend a good tutorial or book on this subject? Thanks! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]