Re: [PHP] Regular Expression
On 5 November 2011 05:39, Negin Nickparsa nickpa...@gmail.com wrote: your welcome my Friend,I used it myself. On 11/4/11, drive view drivev...@gmail.com wrote: Thanks alot Negin for the prompt reply. This is most useful. Regards Best Toni On Sat, Nov 5, 2011 at 6:30 AM, Negin Nickparsa nickpa...@gmail.com wrote: http://weblogtoolscollection.com/regex/regex.php On 11/4/11, drive view drivev...@gmail.com wrote: Hi I'm new to PHP. I'm currently trying understand how to apply the patterns for the preg* commands and I am having difficulty finding a good source explaining in detail how to build a filter. I would appreciate if someone can direct me to such a website. Thanks Toni -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php I use http://www.regular-expressions.info The site's author also has a book which I would recommend : http://www.regular-expressions.info/cookbook.html -- Richard Quadling Twitter : EE : Zend : PHPDoc : Fantasy Shopper @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea : fan.sh/6/370
[PHP] Regular Expression
Hi I'm new to PHP. I'm currently trying understand how to apply the patterns for the preg* commands and I am having difficulty finding a good source explaining in detail how to build a filter. I would appreciate if someone can direct me to such a website. Thanks Toni
Re: [PHP] Regular Expression
http://weblogtoolscollection.com/regex/regex.php On 11/4/11, drive view drivev...@gmail.com wrote: Hi I'm new to PHP. I'm currently trying understand how to apply the patterns for the preg* commands and I am having difficulty finding a good source explaining in detail how to build a filter. I would appreciate if someone can direct me to such a website. Thanks Toni -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression
Thanks alot Negin for the prompt reply. This is most useful. Regards Best Toni On Sat, Nov 5, 2011 at 6:30 AM, Negin Nickparsa nickpa...@gmail.com wrote: http://weblogtoolscollection.com/regex/regex.php On 11/4/11, drive view drivev...@gmail.com wrote: Hi I'm new to PHP. I'm currently trying understand how to apply the patterns for the preg* commands and I am having difficulty finding a good source explaining in detail how to build a filter. I would appreciate if someone can direct me to such a website. Thanks Toni
Re: [PHP] Regular Expression
your welcome my Friend,I used it myself. On 11/4/11, drive view drivev...@gmail.com wrote: Thanks alot Negin for the prompt reply. This is most useful. Regards Best Toni On Sat, Nov 5, 2011 at 6:30 AM, Negin Nickparsa nickpa...@gmail.com wrote: http://weblogtoolscollection.com/regex/regex.php On 11/4/11, drive view drivev...@gmail.com wrote: Hi I'm new to PHP. I'm currently trying understand how to apply the patterns for the preg* commands and I am having difficulty finding a good source explaining in detail how to build a filter. I would appreciate if someone can direct me to such a website. Thanks Toni -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular Expression to get the whole Comma Separated String in Array Key
Hi All, Need help in resolving the below problem- I would like to get the whole comma separated string into an array value- 1. $postText = chapters 5, 6, 7, 8; OR 2. $postText = chapters 5, 6; OR 3. $postText = chapters 5, 6, 7; What i have done so far is- preg_match('/chapter[s]*[ ]*(\d+, \d+, \d+)/i', $postText, $matches); The above will exactly match the third value $postText = chapters 5, 6, 7; By Above $matches will contain a value of : $matches[1] = '5, 6, 7'; Now i need a SINGLE regular expression which can match first, second variable above or any number of comma separated string and IMPORTANTLY provide me that whole comma separated sting value (as above) in single array key element like below- $matches[1] = '5, 6, 7'; OR $matches[1] = '5, 6'; OR $matches[1] = '5, 6, 7, 8, 9'; Also I have to use regular expression only as the flow of the code does not permit to use any other method to get comma separated string from the master/base string. Thanks, Gaurav Kumar
Re: [PHP] regular expression
Peter Lind peter.e.l...@gmail.com wrote in message news:aanlktilqkz8dnc0zacfv70tctf2wqkgpzojccqtuw...@mail.gmail.com... On 1 June 2010 17:33, Ashley Sheridan a...@ashleysheridan.co.uk wrote: On Tue, 2010-06-01 at 16:31 +0100, Richard Quadling wrote: $re1 = '/^[a-z]++$/i'; $re2 = '/^[a-z ]++$/i'; -- - Richard Quadling Standing on the shoulders of some very clever giants! EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling Why the double ++ in the expressions there? Surely one + would match the 1 or more characters that you need and the second one would just be surplus? Equally important: why have three people already done this persons homework. 5 minutes googling would have answered this ... -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind BeWelcome/Couchsurfing: Fake51 Twitter: http://twitter.com/kafe15 /hype i made an regular expression now by myself. i need to check if string starts with 'get' and is followed only by letters a-z case insensitive. am i correct? '/^get[a-z]++$/i' Br Tanel -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression
On Mon, 2010-06-07 at 22:54 +0300, Tanel Tammik wrote: Peter Lind peter.e.l...@gmail.com wrote in message news:aanlktilqkz8dnc0zacfv70tctf2wqkgpzojccqtuw...@mail.gmail.com... On 1 June 2010 17:33, Ashley Sheridan a...@ashleysheridan.co.uk wrote: On Tue, 2010-06-01 at 16:31 +0100, Richard Quadling wrote: $re1 = '/^[a-z]++$/i'; $re2 = '/^[a-z ]++$/i'; -- - Richard Quadling Standing on the shoulders of some very clever giants! EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling Why the double ++ in the expressions there? Surely one + would match the 1 or more characters that you need and the second one would just be surplus? Equally important: why have three people already done this persons homework. 5 minutes googling would have answered this ... -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind BeWelcome/Couchsurfing: Fake51 Twitter: http://twitter.com/kafe15 /hype i made an regular expression now by myself. i need to check if string starts with 'get' and is followed only by letters a-z case insensitive. am i correct? '/^get[a-z]++$/i' Br Tanel Yep, that looks right. If you're in any doubt, there are lots of online regex testers which you can run strings and patterns against. Just Google for them. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] regular expression
On 2 June 2010 06:12, Peter pet...@egrabber.com wrote: Hi Tanel, 1. only letters $str = 'helloworld'; if(preg_match(/^[a-zA-Z]*$/,$str)) echo only letters; else echo failed; 2. only letters and spaces $str = 'hello world'; if(preg_match(/^[a-zA-Z\s]*$/,$str)) echo only letters and spaces; else echo failed; Regards Peter.M Be careful with using *. The issue of a zero length string is important. * will allow a zero length string. ++ will force the regex to match something, so zero length strings are rejected. /s will match space, formfeed, newline, carriage return, horizontal tab, and vertical tab So a string with newlines (for example a textarea with line breaks) would match. -- - Richard Quadling Standing on the shoulders of some very clever giants! EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression
2010/6/1 Peter Lind peter.e.l...@gmail.com: On 1 June 2010 17:33, Ashley Sheridan a...@ashleysheridan.co.uk wrote: On Tue, 2010-06-01 at 16:31 +0100, Richard Quadling wrote: $re1 = '/^[a-z]++$/i'; $re2 = '/^[a-z ]++$/i'; -- - Richard Quadling Standing on the shoulders of some very clever giants! EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling Why the double ++ in the expressions there? Surely one + would match the 1 or more characters that you need and the second one would just be surplus? Equally important: why have three people already done this persons homework. 5 minutes googling would have answered this ... Even more important: No answer is correct, because f.e. äüßćéâ are also letters. Bye ;-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression
On 2 June 2010 16:35, Jan G.B. ro0ot.w...@googlemail.com wrote: 2010/6/1 Peter Lind peter.e.l...@gmail.com: On 1 June 2010 17:33, Ashley Sheridan a...@ashleysheridan.co.uk wrote: On Tue, 2010-06-01 at 16:31 +0100, Richard Quadling wrote: $re1 = '/^[a-z]++$/i'; $re2 = '/^[a-z ]++$/i'; -- - Richard Quadling Standing on the shoulders of some very clever giants! EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling Why the double ++ in the expressions there? Surely one + would match the 1 or more characters that you need and the second one would just be surplus? Equally important: why have three people already done this persons homework. 5 minutes googling would have answered this ... Even more important: No answer is correct, because f.e. äüßćéâ are also letters. Bye ;-) So, would ... /^[^\p{M}\p{Z}\p{N}\p{P}\p{S}\p{C}\d\s]++$/i be ok? -- - Richard Quadling Standing on the shoulders of some very clever giants! EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] regular expression
How to check with regular expression (preg) if string has: 1. only letters 2. only letters and spaces Br Tanel -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression
On Tue, Jun 1, 2010 at 11:19 AM, Tanel Tammik keevit...@gmail.com wrote: How to check with regular expression (preg) if string has: 1. only letters 2. only letters and spaces Br Tanel -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php $re1 = '/^[a-zA-Z]+$/'; $re2 = '/^[a-zA-Z ]+$/'; Adam -- Nephtali: PHP web framework that functions beautifully http://nephtaliproject.com
Re: [PHP] regular expression
On Tue, 2010-06-01 at 18:19 +0300, Tanel Tammik wrote: How to check with regular expression (preg) if string has: 1. only letters 2. only letters and spaces Br Tanel Use preg_match() to find an expression within a string. You can use something like this to grab check if a string contains only letters or numbers: if(preg_match('/^[a-zA-Z0-9]+$/'), $string) { } Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] regular expression
$re1 = '/^[a-z]++$/i'; $re2 = '/^[a-z ]++$/i'; -- - Richard Quadling Standing on the shoulders of some very clever giants! EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression
On Tue, 2010-06-01 at 16:31 +0100, Richard Quadling wrote: $re1 = '/^[a-z]++$/i'; $re2 = '/^[a-z ]++$/i'; -- - Richard Quadling Standing on the shoulders of some very clever giants! EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling Why the double ++ in the expressions there? Surely one + would match the 1 or more characters that you need and the second one would just be surplus? Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] regular expression
On 1 June 2010 16:31, Richard Quadling rquadl...@gmail.com wrote: $re1 = '/^[a-z]++$/i'; ^[a-z]++$ Options: case insensitive; ^ and $ match at line breaks Assert position at the beginning of a line (at beginning of the string or after a line break character) «^» Match a single character in the range between “a” and “z” «[a-z]++» Between one and unlimited times, as many times as possible, without giving back (possessive) «++» Assert position at the end of a line (at the end of the string or before a line break character) «$» $re2 = '/^[a-z ]++$/i'; ^[a-z ]++$ Options: case insensitive; ^ and $ match at line breaks Assert position at the beginning of a line (at beginning of the string or after a line break character) «^» Match a single character present in the list below «[a-z ]++» Between one and unlimited times, as many times as possible, without giving back (possessive) «++» A character in the range between “a” and “z” «a-z» The character “ ” « » Assert position at the end of a line (at the end of the string or before a line break character) «$» -- - Richard Quadling Standing on the shoulders of some very clever giants! EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression
On 1 June 2010 16:33, Ashley Sheridan a...@ashleysheridan.co.uk wrote: On Tue, 2010-06-01 at 16:31 +0100, Richard Quadling wrote: $re1 = '/^[a-z]++$/i'; $re2 = '/^[a-z ]++$/i'; -- - Richard Quadling Standing on the shoulders of some very clever giants! EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling Why the double ++ in the expressions there? Surely one + would match the 1 or more characters that you need and the second one would just be surplus? Thanks, Ash http://www.ashleysheridan.co.uk ++ doesn't give back. As there is no need to do any backtracking, this is supposed to be a slight optimization. -- - Richard Quadling Standing on the shoulders of some very clever giants! EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression
On 1 June 2010 17:33, Ashley Sheridan a...@ashleysheridan.co.uk wrote: On Tue, 2010-06-01 at 16:31 +0100, Richard Quadling wrote: $re1 = '/^[a-z]++$/i'; $re2 = '/^[a-z ]++$/i'; -- - Richard Quadling Standing on the shoulders of some very clever giants! EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling Why the double ++ in the expressions there? Surely one + would match the 1 or more characters that you need and the second one would just be surplus? Equally important: why have three people already done this persons homework. 5 minutes googling would have answered this ... -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind BeWelcome/Couchsurfing: Fake51 Twitter: http://twitter.com/kafe15 /hype -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression
On Tue, 2010-06-01 at 16:35 +0100, Richard Quadling wrote: On 1 June 2010 16:33, Ashley Sheridan a...@ashleysheridan.co.uk wrote: On Tue, 2010-06-01 at 16:31 +0100, Richard Quadling wrote: $re1 = '/^[a-z]++$/i'; $re2 = '/^[a-z ]++$/i'; -- - Richard Quadling Standing on the shoulders of some very clever giants! EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling Why the double ++ in the expressions there? Surely one + would match the 1 or more characters that you need and the second one would just be surplus? Thanks, Ash http://www.ashleysheridan.co.uk ++ doesn't give back. As there is no need to do any backtracking, this is supposed to be a slight optimization. -- - Richard Quadling Standing on the shoulders of some very clever giants! EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling Ah, I ought to have guessed as it's you! ;) I didn't know about that in regex's, I've learnt something new today! Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] regular expression
On 1 June 2010 16:38, Ashley Sheridan a...@ashleysheridan.co.uk wrote: Ah, I ought to have guessed as it's you! ;) What are you insinuating? -- - Richard Quadling Standing on the shoulders of some very clever giants! EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression
On Tue, 2010-06-01 at 17:32 +0100, Richard Quadling wrote: On 1 June 2010 16:38, Ashley Sheridan a...@ashleysheridan.co.uk wrote: Ah, I ought to have guessed as it's you! ;) What are you insinuating? -- - Richard Quadling Standing on the shoulders of some very clever giants! EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling I've never known anyone to optimise code quite as much as you! That's not a bad thing though. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] regular expression
Hi Tanel, 1. only letters $str = 'helloworld'; if(preg_match(/^[a-zA-Z]*$/,$str)) echo only letters; else echo failed; 2. only letters and spaces $str = 'hello world'; if(preg_match(/^[a-zA-Z\s]*$/,$str)) echo only letters and spaces; else echo failed; Regards Peter.M Tanel Tammik wrote: How to check with regular expression (preg) if string has: 1. only letters 2. only letters and spaces Br Tanel -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular Expression Problem
Hi, I have two lines here as follows: 1 -0.123701962557954E+03 0.460967618024691E+02 -0.12354765900E+03 0.46259109000E+02 What I am trying to do here is to only have 1 -0.123701962557954E+03 0.460967618024691E+02 be the output so I can do further processing with it, however, with the code I have in the following, I have both lines in the output. Here is the code: $file = test.txt; $fp = fopen($file, r); $lines = file($file); foreach($lines as $line_num = $line) { if (preg_match(/([^0-9]+\s+)(\-?\d+\.?\d+(\+?E?\d+)?){2}/, $line)) echo $line; } Could anyone please tell me that even when I specify that the line has to start with [^0-9]+\s with one occurrence, why I still get -0.12354765900E+03 0.46259109000E+02? Thanks a lot for your help. Alice _ Express yourself with gadgets on Windows Live Spaces http://discoverspaces.live.com?source=hmtag1loc=us
Re: [PHP] Regular Expression Problem
Alice Wei wrote: Hi, I have two lines here as follows: 1 -0.123701962557954E+03 0.460967618024691E+02 -0.12354765900E+03 0.46259109000E+02 What I am trying to do here is to only have 1 -0.123701962557954E+03 0.460967618024691E+02 be the output so I can do further processing with it, however, with the code I have in the following, I have both lines in the output. Here is the code: $file = "test.txt"; $fp = fopen($file, "r"); $lines = file($file); foreach($lines as $line_num = $line) { if (preg_match("/([^0-9]+\s+)(\-?\d+\.?\d+(\+?E?\d+)?){2}/", $line)) echo $line; } Could anyone please tell me that even when I specify that the line has to start with [^0-9]+\s with one occurrence, why I still get -0.12354765900E+03 0.46259109000E+02? Thanks a lot for your help. Alice _ Express yourself with gadgets on Windows Live Spaces http://discoverspaces.live.com?source=hmtag1loc=us [^0-9] means not a digital ^[0-9] is the right way.. -- Laruence's Signature 惠 新宸 xinchen.hui | SYS | (+8610)82602112-7974 | :laruence
RE: [PHP] Regular Expression Problem
Hi, I have two lines here as follows: 1 -0.123701962557954E+03 0.460967618024691E+02 -0.12354765900E+03 0.46259109000E+02 What I am trying to do here is to only have 1 -0.123701962557954E+03 0.460967618024691E+02 be the output so I can do further processing with it, however, with the code I have in the following, I have both lines in the output. Here is the code: $file = test.txt; $fp = fopen($file, r); $lines = file($file); foreach($lines as $line_num = $line) { if (preg_match(/([^0-9]+\s+)(\-?\d+\.?\d+(\+?E?\d+)?){2}/, $line)) echo $line; } Could anyone please tell me that even when I specify that the line has to start with [^0-9]+\s with one occurrence, why I still get -0.12354765900E+03 0.46259109000E+02? Thanks a lot for your help. Alice _ Express yourself with gadgets on Windows Live Spaces http://discoverspaces.live.com?source=hmtag1loc=us [^0-9] means not a digital ^[0-9] is the right way.. Thanks for your help. I cannot imagine that I have missed that. Alice _ Use Messenger to talk to your IM friends, even those on Yahoo! http://ideas.live.com/programpage.aspx?versionId=7adb59de-a857-45ba-81cc-685ee3e858fe
Re: [PHP] Regular Expression Problem
On Thu, 2009-02-26 at 10:09 -0500, Alice Wei wrote: Hi, I have two lines here as follows: 1 -0.123701962557954E+03 0.460967618024691E+02 -0.12354765900E+03 0.46259109000E+02 What I am trying to do here is to only have 1 -0.123701962557954E+03 0.460967618024691E+02 be the output so I can do further processing with it, however, with the code I have in the following, I have both lines in the output. Here is the code: $file = test.txt; $fp = fopen($file, r); $lines = file($file); foreach($lines as $line_num = $line) { if (preg_match(/([^0-9]+\s+)(\-?\d+\.?\d+(\+?E?\d+)?){2}/, $line)) echo $line; } Could anyone please tell me that even when I specify that the line has to start with [^0-9]+\s with one occurrence, why I still get -0.12354765900E+03 0.46259109000E+02? Thanks a lot for your help. Alice _ Express yourself with gadgets on Windows Live Spaces http://discoverspaces.live.com?source=hmtag1loc=us what about a regex something like: /^(\d{1,})\s+([\d\.E\+\-])+\s+([\d\.E\+\-])+$/ The brackets () allow you to then extract the specific portions of the string as you wish, so the first match is the whole line, the second is the 1 (or whatever other number), the third is the first floating-point number, etc. I'm assuming that the 1 here is some sort of line number possibly? The bit inside the curly braces ({1,}) just says to match the digit 1 or more times. You can limit it by adding a number after the comma, so {1,3} for 1-3 digits. Hope this helps? Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular Expression Backreference in subpattern.
The string is tdcharge/tdtd100/td. I want and array( charge=100). I am using this regular expression, '/td([^]+)\/tdtd(?P\1\d+)\/td/'. But its not working.. I get this error., PHP Warning: preg_match(): Compilation failed: syntax error after (?P at offset 25 in E:\src\php\WebEngine\- on line 4 any idea? -- Blog: http://talk.cmyweb.net/ Follow me: http://twitter.com/shiplu -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Regular Expression Backreference in subpattern.
Not sure what you think the (?P is doing, but it looks very suspicious to me... I'm no PCRE expert though... Try this: '|td\\s*charge\\s*/td\\s*td\\s*([0-9]*)\\s*/td|' \\s allows for whitespace If you only want ones that HAVE to have numbers, and no blanks, change * after the 0-9] bit into + From: Shiplu [EMAIL PROTECTED] Sent: Saturday, September 27, 2008 10:24 AM To: PHP General Subject: [PHP] Regular Expression Backreference in subpattern. The string is tdcharge/tdtd100/td. I want and array( charge=100). I am using this regular expression, '/td([^]+)\/tdtd(?P\1\d+)\/td/'. But its not working.. I get this error., PHP Warning: preg_match(): Compilation failed: syntax error after (?P at offset 25 in E:\src\php\WebEngine\- on line 4 any idea? -- Blog: http://talk.cmyweb.net/ Follow me: http://twitter.com/shiplu -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php ___ The information in this email or in any file attached hereto is intended only for the personal and confiden- tial use of the individual or entity to which it is addressed and may contain information that is propri- etary and confidential. If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited. This communica- tion is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product. Email trans- mission cannot be guaranteed to be secure or error- free. P6070214 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression Backreference in subpattern.
On 9/27/08, Richard Lynch [EMAIL PROTECTED] wrote: Not sure what you think the (?P is doing, but it looks very suspicious to me... I'm no PCRE expert though... Try this: '|td\\s*charge\\s*/td\\s*td\\s*([0-9]*)\\s*/td|' \\s allows for whitespace If you only want ones that HAVE to have numbers, and no blanks, change * after the 0-9] bit into + It doesnt need \\s or \s. Because I have given the string. \s* is okay though. Well, ?P is 100% okay with PCRE. it gives name subpattern matches (details: http://www.php.net/manual/en/regexp.reference.php); -- Blog: http://talk.cmyweb.net/ Follow me: http://twitter.com/shiplu -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular Expression differences between 4.4 and 5.2
Hi everybody, I'm trying to migrate our PHP code base to PHP5. For the most part the transition hs been smooth however I'm really stuck on a regular expression which gives different results in 4.4 and 5.2. I've looked through migration guides but as far as I can see nothing should have changed in Regex handling between the versions. I've created the following script below to test the match: ?php include( 'input.inc' ); $pattern = /!T_([^ ]+)([^]*)(.*?)!T_end\\1|!T_([^ ]+)([^]*)/si; preg_match_all( $pattern, $string, $matches ); echo phpversion(); var_dump($matches); ? Input.inc contains the string that is giving the different results - its probably to long to include so you can find it at http://www.workingweb.nl/example/input.inc. The results of this being run can be seen at: PHP4 (desired result) - http://www.workingweb.nl/example/php4results PHP5 - http://www.workingweb.nl/example/php5results If the pattern modifier 's' is removed the regex behaves consistently again (but this breaks other pages on our website so can't be used as a workaround). Besides I don't see why that should make any difference. Here is the version information from the PHP CLI versions I've been testing with: [EMAIL PROTECTED]:~$ php4 -v PHP 4.4.4-8+etch6 (cli) (built: May 16 2008 15:59:34) Copyright (c) 1997-2006 The PHP Group Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies [EMAIL PROTECTED]:~$ php5 -v PHP 5.2.0-8+etch11 (cli) (built: May 10 2008 10:46:24) Copyright (c) 1997-2006 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2006 Zend Technologies Any help on this would be very appreciated. Thanks, Ewen
Re: [PHP] Regular Expression differences between 4.4 and 5.2
Ewen Cumming schreef: Hi everybody, ... ?php include( 'input.inc' ); $pattern = /!T_([^ ]+)([^]*)(.*?)!T_end\\1|!T_([^ ]+)([^]*)/si; preg_match_all( $pattern, $string, $matches ); echo phpversion(); var_dump($matches); ? Input.inc contains the string that is giving the different results - its probably to long to include so you can find it at http://www.workingweb.nl/example/input.inc. The results of this being run can be seen at: PHP4 (desired result) - http://www.workingweb.nl/example/php4results PHP5 - http://www.workingweb.nl/example/php5results If the pattern modifier 's' is removed the regex behaves consistently again (but this breaks other pages on our website so can't be used as a workaround). Besides I don't see why that should make any difference. it seem you have found a regression, submit a bug with a short repro script (cut down the test HTML and include it in the script). I tested with php 5.2.6, it shows the same behaviour as 5.2.0. BUT I may have work around for you, try this regexp (replaces s modifer with m modifier): $pattern = /!T_([^ ]+)([^]*)(.*?)!T_end\\1|!T_([^ ]+)([^]*)/mi; my test script was: ?php include( 'http://www.workingweb.nl/example/input.inc' ); // $pattern = '#(?:!T_([^ ]+)([^]*)(.*?)!T_end\\1|!T_([^ ]+)([^]*))#mi'; $pattern = /!T_([^ ]+)([^]*)(.*?)!T_end\\1|!T_([^ ]+)([^]*)/mi; preg_match_all( $pattern, $string, $matches ); echo phpversion(); var_dump($matches); Here is the version information from the PHP CLI versions I've been testing with: [EMAIL PROTECTED]:~$ php4 -v PHP 4.4.4-8+etch6 (cli) (built: May 16 2008 15:59:34) Copyright (c) 1997-2006 The PHP Group Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies [EMAIL PROTECTED]:~$ php5 -v PHP 5.2.0-8+etch11 (cli) (built: May 10 2008 10:46:24) Copyright (c) 1997-2006 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2006 Zend Technologies Any help on this would be very appreciated. Thanks, Ewen -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression differences between 4.4 and 5.2
Hi Jochem, Replacing the 's' modifier with 'm' fixed it this instance but broke other parts on the site (the same result as removing 's'). But the other regex ( $pattern = /!T_([^ ]+)([^]*)(.*?)!T_end\\1|!T_([^ ]+)([^]*)/Ui;) is working perfectly. I will continue to test and see if it throws up any other problems. Many thanks for such a quick and great response. I will file a bug report however I may need to submit the full test string as cutting it down any further seems to 'fix' the discrepency. Thanks again, Ewen 2008/9/12 Jochem Maas [EMAIL PROTECTED] Jochem Maas schreef: Ewen Cumming schreef: Hi everybody, ... BUT I may have work around for you, try this regexp (replaces s modifer with m modifier): $pattern = /!T_([^ ]+)([^]*)(.*?)!T_end\\1|!T_([^ ]+)([^]*)/mi; the following pattern also seems to do what you want: $pattern = /!T_([^ ]+)([^]*)(.*?)!T_end\\1|!T_([^ ]+)([^]*)/Ui; Im interested to know if either of these two solve your immediate issue.
Re: [PHP] Regular Expression differences between 4.4 and 5.2
Jochem Maas schreef: Ewen Cumming schreef: Hi everybody, ... BUT I may have work around for you, try this regexp (replaces s modifer with m modifier): $pattern = /!T_([^ ]+)([^]*)(.*?)!T_end\\1|!T_([^ ]+)([^]*)/mi; the following pattern also seems to do what you want: $pattern = /!T_([^ ]+)([^]*)(.*?)!T_end\\1|!T_([^ ]+)([^]*)/Ui; Im interested to know if either of these two solve your immediate issue. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression differences between 4.4 and 5.2
Actually bummer - testing on wrong version. The U modifier is causing problems too - only matching the first character instead of the whole string. 2008/9/12 Ewen Cumming [EMAIL PROTECTED] Hi Jochem, Replacing the 's' modifier with 'm' fixed it this instance but broke other parts on the site (the same result as removing 's'). But the other regex ( $pattern = /!T_([^ ]+)([^]*)(.*?)!T_end\\1|!T_([^ ]+)([^]*)/Ui;) is working perfectly. I will continue to test and see if it throws up any other problems. Many thanks for such a quick and great response. I will file a bug report however I may need to submit the full test string as cutting it down any further seems to 'fix' the discrepency. Thanks again, Ewen 2008/9/12 Jochem Maas [EMAIL PROTECTED] Jochem Maas schreef: Ewen Cumming schreef: Hi everybody, ... BUT I may have work around for you, try this regexp (replaces s modifer with m modifier): $pattern = /!T_([^ ]+)([^]*)(.*?)!T_end\\1|!T_([^ ]+)([^]*)/mi; the following pattern also seems to do what you want: $pattern = /!T_([^ ]+)([^]*)(.*?)!T_end\\1|!T_([^ ]+)([^]*)/Ui; Im interested to know if either of these two solve your immediate issue.
Re: [PHP] Regular Expression differences between 4.4 and 5.2
Ewen Cumming schreef: Actually bummer - testing on wrong version. The U modifier is causing problems too - only matching the first character instead of the whole string. hmm, missed that, that might require adding ?U to relevant sub-assertion ... I'm wondering if your regexp is not 'wrong' (big word in this case), try this variation: $pattern = '#\\!T_([^ ]+)([^]*)(?:(.*)?\\!T_end\\1)?#si'; gives me the same output as your expected results, albeit that there are less 'root' keys in the matches array ... so you may need to change some code with regard to fishing out values from $matches 2008/9/12 Ewen Cumming [EMAIL PROTECTED] Hi Jochem, Replacing the 's' modifier with 'm' fixed it this instance but broke other parts on the site (the same result as removing 's'). But the other regex ( $pattern = /!T_([^ ]+)([^]*)(.*?)!T_end\\1|!T_([^ ]+)([^]*)/Ui;) is working perfectly. I will continue to test and see if it throws up any other problems. Many thanks for such a quick and great response. I will file a bug report however I may need to submit the full test string as cutting it down any further seems to 'fix' the discrepency. Thanks again, Ewen 2008/9/12 Jochem Maas [EMAIL PROTECTED] Jochem Maas schreef: Ewen Cumming schreef: Hi everybody, ... BUT I may have work around for you, try this regexp (replaces s modifer with m modifier): $pattern = /!T_([^ ]+)([^]*)(.*?)!T_end\\1|!T_([^ ]+)([^]*)/mi; the following pattern also seems to do what you want: $pattern = /!T_([^ ]+)([^]*)(.*?)!T_end\\1|!T_([^ ]+)([^]*)/Ui; Im interested to know if either of these two solve your immediate issue. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular Expression by Exception
Fellows: If I use ereg_replace($params, $string) I can replace the char that match with $params in the $string. BUT, how I can replace ALL chars EXCEPT the chars in $params. Something like !$params, I think -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression by Exception
I don't know how to use the POSIX classes, but if you use preg_replace: preg_replace(/[^$params]/, '', $string); I think this will work. Thank you, Micah Gersten onShore Networks Internal Developer http://www.onshore.com Alberto García Gómez wrote: Fellows: If I use ereg_replace($params, $string) I can replace the char that match with $params in the $string. BUT, how I can replace ALL chars EXCEPT the chars in $params. Something like !$params, I think -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression help need
Before: phi strongRichard/strong, good morninglt;/p After: phi strongRichard/stronggt;, amp; good morninglt;/p By the sounds of it negative look ahead assertions may be of some help. Or look behind assertions. -- Richard Heyes http://www.phpguru.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression help need
On Fri, Jul 25, 2008 at 1:08 PM, Shelley [EMAIL PROTECTED] wrote: Hi Richard, Not exactly actually. What I mean is: Before: phi strongRichard/strong, good morninglt;/p After: phi strongRichard/stronggt;, amp; good morninglt;/p I hope it's clear now. On Fri, Jul 25, 2008 at 7:53 PM, Richard Heyes [EMAIL PROTECTED] wrote: How can I make a string with (NOT amp;, gt;, lt; or quot;), , xml compatible? What is the expression to use? Not entirely sure what you're after (try posting some before and after snippets), but by the sounds of it you don't need a regular expression - strtr() will work for you. Or str_replace(). -- Richard Heyes http://www.phpguru.org -- Regards, Shelley Is it possible for you to use a mixture of html_entity_decode, htmlentities e.g. ?php $content = html_entity_decode(', good morninglt;'); echo 'phi strongRichard/strong'.htmlentities($content).'/p'; /James
Re: [PHP] Regular Expression help need
Are you talking about looking at blogs in a mobile phone browser or actually downloading the blog into another format? Thank you, Micah Gersten onShore Networks Internal Developer http://www.onshore.com Shelley wrote: Ok, let me tell you what i want to achieve. I want to transfer users' blog onto mobile phone, so I should convert characters such as , , (but not amp;, or gt;, or lt;, etc) into xml compatible ones, gt;, lt; amp;. Maybe there is some problem in my expression? Waiting for your response... On Sat, Jul 26, 2008 at 3:30 AM, Micah Gersten [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Are you trying to make it xml compatible or XHTML compatible? '' is not valid HTML or XHTML as it has special meaning. If you want it to adhere to the standard and display correctly, you must use 'amp;' Thank you, Micah Gersten onShore Networks Internal Developer http://www.onshore.com Shelley wrote: Hi Richard, Not exactly actually. What I mean is: Before: phi strongRichard/strong, good morninglt;/p After: phi strongRichard/stronggt;, amp; good morninglt;/p I hope it's clear now. On Fri, Jul 25, 2008 at 7:53 PM, Richard Heyes [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: How can I make a string with (NOT amp;, gt;, lt; or quot;), , xml compatible? What is the expression to use? Not entirely sure what you're after (try posting some before and after snippets), but by the sounds of it you don't need a regular expression - strtr() will work for you. Or str_replace(). -- Richard Heyes http://www.phpguru.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- With best regards, Shelley Shyan http://phparch.cn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular Expression help need
Hi all, How can I make a string with (NOT amp;, gt;, lt; or quot;), , xml compatible? What is the expression to use? Thank you very much. -- Regards, Shelley
Re: [PHP] Regular Expression help need
How can I make a string with (NOT amp;, gt;, lt; or quot;), , xml compatible? What is the expression to use? Not entirely sure what you're after (try posting some before and after snippets), but by the sounds of it you don't need a regular expression - strtr() will work for you. Or str_replace(). -- Richard Heyes http://www.phpguru.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression help need
Hi Richard, Not exactly actually. What I mean is: Before: phi strongRichard/strong, good morninglt;/p After: phi strongRichard/stronggt;, amp; good morninglt;/p I hope it's clear now. On Fri, Jul 25, 2008 at 7:53 PM, Richard Heyes [EMAIL PROTECTED] wrote: How can I make a string with (NOT amp;, gt;, lt; or quot;), , xml compatible? What is the expression to use? Not entirely sure what you're after (try posting some before and after snippets), but by the sounds of it you don't need a regular expression - strtr() will work for you. Or str_replace(). -- Richard Heyes http://www.phpguru.org -- Regards, Shelley
Re: [PHP] Regular Expression help need
Are you trying to make it xml compatible or XHTML compatible? '' is not valid HTML or XHTML as it has special meaning. If you want it to adhere to the standard and display correctly, you must use 'amp;' Thank you, Micah Gersten onShore Networks Internal Developer http://www.onshore.com Shelley wrote: Hi Richard, Not exactly actually. What I mean is: Before: phi strongRichard/strong, good morninglt;/p After: phi strongRichard/stronggt;, amp; good morninglt;/p I hope it's clear now. On Fri, Jul 25, 2008 at 7:53 PM, Richard Heyes [EMAIL PROTECTED] wrote: How can I make a string with (NOT amp;, gt;, lt; or quot;), , xml compatible? What is the expression to use? Not entirely sure what you're after (try posting some before and after snippets), but by the sounds of it you don't need a regular expression - strtr() will work for you. Or str_replace(). -- Richard Heyes http://www.phpguru.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression help need
Ok, let me tell you what i want to achieve. I want to transfer users' blog onto mobile phone, so I should convert characters such as , , (but not amp;, or gt;, or lt;, etc) into xml compatible ones, gt;, lt; amp;. Maybe there is some problem in my expression? Waiting for your response... On Sat, Jul 26, 2008 at 3:30 AM, Micah Gersten [EMAIL PROTECTED] wrote: Are you trying to make it xml compatible or XHTML compatible? '' is not valid HTML or XHTML as it has special meaning. If you want it to adhere to the standard and display correctly, you must use 'amp;' Thank you, Micah Gersten onShore Networks Internal Developer http://www.onshore.com Shelley wrote: Hi Richard, Not exactly actually. What I mean is: Before: phi strongRichard/strong, good morninglt;/p After: phi strongRichard/stronggt;, amp; good morninglt;/p I hope it's clear now. On Fri, Jul 25, 2008 at 7:53 PM, Richard Heyes [EMAIL PROTECTED] wrote: How can I make a string with (NOT amp;, gt;, lt; or quot;), , xml compatible? What is the expression to use? Not entirely sure what you're after (try posting some before and after snippets), but by the sounds of it you don't need a regular expression - strtr() will work for you. Or str_replace(). -- Richard Heyes http://www.phpguru.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- With best regards, Shelley Shyan http://phparch.cn
Re: [PHP] regular expression to find body text in mobile
The server recieve email from mobile phone. I would like to retrieve the text of body of mobile phone, which recieve email from mobile phone email. Of cource, server run php and is able to use regular expression. But the email, which is recieved by mobile phone, retrieve text body on server. Regards, Yui 2008/6/12 Nitsan Bin-Nun [EMAIL PROTECTED]: I'm confused. PHP runs on the server, so it shouldn't be a problem at all to run regex search/reaplce/match/whatever on mobile phone internet, maybe you are talking on JS regex? Nitsan On 11/06/2008, Yui Hiroaki [EMAIL PROTECTED] wrote: Doese any know how to find text in mobile using Regular Expression? I am using php. Regards, Yui -- 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] regular expression to find body text in mobile
Doese any know how to find text in mobile using Regular Expression? I am using php. Regards, Yui -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression to find body text in mobile
Doese any know how to find text in mobile using Regular Expression? I am using php. Mobile what? -- Richard Heyes Employ me: http://www.phpguru.org/cv ++ | Access SSH with a Windows mapped drive | |http://www.phpguru.org/sftpdrive| ++ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression to find body text in mobile
Ooops! It it mobile mobile phone! Regards, Yui 2008/6/11 Richard Heyes [EMAIL PROTECTED]: Doese any know how to find text in mobile using Regular Expression? I am using php. Mobile what? -- Richard Heyes Employ me: http://www.phpguru.org/cv ++ | Access SSH with a Windows mapped drive | |http://www.phpguru.org/sftpdrive| ++ -- 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] regular expression to find body text in mobile
On Wed, Jun 11, 2008 at 9:43 AM, Yui Hiroaki [EMAIL PROTECTED] wrote: Ooops! It it mobile mobile phone! Regards, Yui 2008/6/11 Richard Heyes [EMAIL PROTECTED]: Doese any know how to find text in mobile using Regular Expression? I am using php. Mobile what? -- Richard Heyes You want to search for text that is on a screen on a mobile device? PHP runs on the server, so it won't be able to manage that. Can you be more clear about the goal? Help us understand what you want to do. -- Bastien Cat, the other other white meat
Re: [PHP] regular expression to find body text in mobile
I'm confused. PHP runs on the server, so it shouldn't be a problem at all to run regex search/reaplce/match/whatever on mobile phone internet, maybe you are talking on JS regex? Nitsan On 11/06/2008, Yui Hiroaki [EMAIL PROTECTED] wrote: Doese any know how to find text in mobile using Regular Expression? I am using php. Regards, Yui -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression question
But how? The +[a-z]{2,} seems to allow at least two a-z clusters, but it doesn't include a period. /ml Almost correct. The plus belongs to whatever comes before it, not after. So what you're referring to as matching two or more characters but not the period, is this: [a-z]{2,} And this will match one or more subdomains: ([-a-z0-9]+\.)+ -- Richard Heyes +44 (0)800 0213 172 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] regular expression question
Hello: I've adapted this regular expression script from a book, but I'm not clear why it works. $email = [EMAIL PROTECTED]; $pattern = /[EMAIL PROTECTED]@([-a-z0-9]+\.)+[a-z]{2,}/; ___ if ( preg_match($pattern,$email) ) { print yes! . $email . matches!; } else { print no match; } ___ When I run this script, I get the yes! [EMAIL PROTECTED] matches! statement. But I don't understand why the second half of the regular expression works. I'm talking about this part: @([-a-z0-9]+\.)+[a-z]{2,}/; why is it able to detect repeated sections of the email address after @ that are separated by periods? like @email.alaska.com . It looks to me like it's only looking for one example of that pattern. Does the () allow an unlimited number of patterns to pass? thanks for any and all guidance Matthew Matthew Lasar || llfcc.net -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression question
Matthew Lasar wrote: But I don't understand why the second half of the regular expression works. I'm talking about this part: @([-a-z0-9]+\.)+[a-z]{2,}/; why is it able to detect repeated sections of the email address after @ that are separated by periods? like @email.alaska.com . It looks to me like it's only looking for one example of that pattern. Does the () allow an unlimited number of patterns to pass? No, but the following '+' does. /Per Jessen, Zürich -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression question
At 11:32 AM 8/31/2007, Per Jessen wrote: Matthew Lasar wrote: But I don't understand why the second half of the regular expression works. I'm talking about this part: @([-a-z0-9]+\.)+[a-z]{2,}/; why is it able to detect repeated sections of the email address after @ that are separated by periods? like @email.alaska.com . It looks to me like it's only looking for one example of that pattern. Does the () allow an unlimited number of patterns to pass? No, but the following '+' does. But how? The +[a-z]{2,} seems to allow at least two a-z clusters, but it doesn't include a period. /ml /Per Jessen, Zürich -- 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 Free Edition. Version: 7.5.484 / Virus Database: 269.13.0/980 - Release Date: 8/30/2007 6:05 PM -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression question
Matthew Lasar wrote: At 11:32 AM 8/31/2007, Per Jessen wrote: Matthew Lasar wrote: But I don't understand why the second half of the regular expression works. I'm talking about this part: @([-a-z0-9]+\.)+[a-z]{2,}/; why is it able to detect repeated sections of the email address after @ that are separated by periods? like @email.alaska.com . It looks to me like it's only looking for one example of that pattern. Does the () allow an unlimited number of patterns to pass? No, but the following '+' does. But how? The +[a-z]{2,} seems to allow at least two a-z clusters, but it doesn't include a period. /ml That plus applies to the grouping () before it: ([-a-z0-9]+\.)+ one or more sequences of -a-z0-9 followed by a period /Per Jessen, Zürich -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Regular expression - URL validator
Thanks again Jim, That's what i really need. I'm testing this function... If i put a URL like www.example.com, then it works fine and turns it to http://www.example.com But if i put a URL like http://www.example.com, then it also put another header so it turns to http://http://www.example.com I also tried with the strstr function, but receive the same response. Thanks in advance, Wagner. -Original Message- From: Jim Lucas [mailto:[EMAIL PROTECTED] Sent: terça-feira, 28 de agosto de 2007 18:35 To: [EMAIL PROTECTED] Cc: PHP General Subject: Re: [PHP] Regular expression - URL validator Wagner Garcia Campagner wrote: Thanks Jim, Your sugestion worked perfect for me!! I have another question: After i validate this URL i want to put a link with this URL in my page. The problem is that if the URL is like (www.aol.com), when i create the link, this URL is appended with the URL of my site. The result is a link pointing to: http://mywebsite/www.aol.com But if the URL is like (http://aol.com), then the link is created correct. Is there a way to avoid the first situation... so the link is created correct? Thanks again, Wagner. You could always do a string comparison for http(s)? in the url if ( strpos($url, array('https://', 'http://')) === false ) { $url = 'http://'.$url; } -- 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] Regular expression - URL validator
Wagner Garcia Campagner wrote: Thanks again Jim, That's what i really need. I'm testing this function... If i put a URL like www.example.com, then it works fine and turns it to http://www.example.com But if i put a URL like http://www.example.com, then it also put another header so it turns to http://http://www.example.com I also tried with the strstr function, but receive the same response. Thanks in advance, Wagner. Give this a shot plaintext?php function fixurl($url) { if ( !preg_match('!^(ftp|http(s)?)://!', $url) ) { $url = 'http://'.$url; } return $url; } echo fixurl(www.google.com); echo \n; echo fixurl(ftp://www.google.com;); echo \n; echo fixurl(http://www.google.com;); echo \n; echo fixurl(https://www.google.com;); ? -- 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] Regular expression - URL validator
Yes, make sure you have http:// on the front. ?php if (strtolower(substr($link, 0, 7)) != 'http://') $link = http://$link;; ? On Tue, August 28, 2007 3:22 pm, Wagner Garcia Campagner wrote: Thanks Jim, Your sugestion worked perfect for me!! I have another question: After i validate this URL i want to put a link with this URL in my page. The problem is that if the URL is like (www.aol.com), when i create the link, this URL is appended with the URL of my site. The result is a link pointing to: http://mywebsite/www.aol.com But if the URL is like (http://aol.com), then the link is created correct. Is there a way to avoid the first situation... so the link is created correct? Thanks again, Wagner. -Original Message- From: Jim Lucas [mailto:[EMAIL PROTECTED] Sent: segunda-feira, 27 de agosto de 2007 17:36 To: PHP General; [EMAIL PROTECTED] Subject: Re: [PHP] Regular expression - URL validator Wagner Garcia Campagner wrote: Hello, I found this regular expression on a web site. It is basicaly an URL validator. I'm trying to implement this in my web site, but i receive errors. I think this is a PERL REGEX so what should i do to make it work in php? $valid = (preg_match('^H|h)(T|t)|(F|f))(T|t)(P|p)((S|s)?))\://)?(www.|[a-zA-Z0-9] .)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,6}(\:[0-9]{1,5})*(/($|[a-zA-Z0-9\.\,\;\?\'\\\ +amp;%\$#\=~_\-]+))*$', $_POST['website'])); This should be preg_match('/.../i', $_POST['website']) your regex should look something like this. ^((ftp|(http(s)?))://)?(\.?([a-z0-9-]+))+\.[a-z]{2,6}(:[0-9]{1,5})?(/[a-zA-Z 0-9.,;\?|\'+%\$#=~_-]+)*$ So, put it all together and it should look like this. ?php $url = ...PUT YOUR TEST URL HERE...; if ( preg_match('!^((ftp|(http(s)?))://)?(\.?([a-z0-9-]+))+\.[a-z]{2,6}(:[0-9]{1, 5})?(/[a-zA-Z0-9.,;\?|\'+%\$#=~_-]+)*$!i', $url) ) { echo Matched; } else { echo Did not match; } if ($valido == 0) { something here; } else { something else here; } Thanks a lot in advance, Wagner. -- 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 -- Please vote for this great band: http://acl.mp3.com/feature/soundandjury/?band=COMPANY-OF-THIEVES Requires email confirmation. One vote per day per email limit. Obvious ballot-stuffing will be revoked. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Regular expression - URL validator
Thanks Jim, Your sugestion worked perfect for me!! I have another question: After i validate this URL i want to put a link with this URL in my page. The problem is that if the URL is like (www.aol.com), when i create the link, this URL is appended with the URL of my site. The result is a link pointing to: http://mywebsite/www.aol.com But if the URL is like (http://aol.com), then the link is created correct. Is there a way to avoid the first situation... so the link is created correct? Thanks again, Wagner. -Original Message- From: Jim Lucas [mailto:[EMAIL PROTECTED] Sent: segunda-feira, 27 de agosto de 2007 17:36 To: PHP General; [EMAIL PROTECTED] Subject: Re: [PHP] Regular expression - URL validator Wagner Garcia Campagner wrote: Hello, I found this regular expression on a web site. It is basicaly an URL validator. I'm trying to implement this in my web site, but i receive errors. I think this is a PERL REGEX so what should i do to make it work in php? $valid = (preg_match('^H|h)(T|t)|(F|f))(T|t)(P|p)((S|s)?))\://)?(www.|[a-zA-Z0-9] .)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,6}(\:[0-9]{1,5})*(/($|[a-zA-Z0-9\.\,\;\?\'\\\ +amp;%\$#\=~_\-]+))*$', $_POST['website'])); This should be preg_match('/.../i', $_POST['website']) your regex should look something like this. ^((ftp|(http(s)?))://)?(\.?([a-z0-9-]+))+\.[a-z]{2,6}(:[0-9]{1,5})?(/[a-zA-Z 0-9.,;\?|\'+%\$#=~_-]+)*$ So, put it all together and it should look like this. ?php $url = ...PUT YOUR TEST URL HERE...; if ( preg_match('!^((ftp|(http(s)?))://)?(\.?([a-z0-9-]+))+\.[a-z]{2,6}(:[0-9]{1, 5})?(/[a-zA-Z0-9.,;\?|\'+%\$#=~_-]+)*$!i', $url) ) { echo Matched; } else { echo Did not match; } if ($valido == 0) { something here; } else { something else here; } Thanks a lot in advance, Wagner. -- 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] Regular expression - URL validator
On 8/28/07, Wagner Garcia Campagner [EMAIL PROTECTED] wrote: Thanks Jim, Your sugestion worked perfect for me!! I have another question: After i validate this URL i want to put a link with this URL in my page. The problem is that if the URL is like (www.aol.com), when i create the link, this URL is appended with the URL of my site. The result is a link pointing to: http://mywebsite/www.aol.com But if the URL is like (http://aol.com), then the link is created correct. Is there a way to avoid the first situation... so the link is created correct? Thanks again, Wagner. -Original Message- From: Jim Lucas [mailto:[EMAIL PROTECTED] Sent: segunda-feira, 27 de agosto de 2007 17:36 To: PHP General; [EMAIL PROTECTED] Subject: Re: [PHP] Regular expression - URL validator Wagner Garcia Campagner wrote: Hello, I found this regular expression on a web site. It is basicaly an URL validator. I'm trying to implement this in my web site, but i receive errors. I think this is a PERL REGEX so what should i do to make it work in php? $valid = (preg_match('^H|h)(T|t)|(F|f))(T|t)(P|p)((S|s)?))\://)?(www.|[a-zA-Z0-9] .)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,6}(\:[0-9]{1,5})*(/($|[a-zA-Z0-9\.\,\;\?\'\\\ +amp;%\$#\=~_\-]+))*$', $_POST['website'])); This should be preg_match('/.../i', $_POST['website']) your regex should look something like this. ^((ftp|(http(s)?))://)?(\.?([a-z0-9-]+))+\.[a-z]{2,6}(:[0-9]{1,5})?(/[a-zA-Z 0-9.,;\?|\'+%\$#=~_-]+)*$ So, put it all together and it should look like this. ?php $url = ...PUT YOUR TEST URL HERE...; if ( preg_match('!^((ftp|(http(s)?))://)?(\.?([a-z0-9-]+))+\.[a-z]{2,6}(:[0-9]{1, 5})?(/[a-zA-Z0-9.,;\?|\'+%\$#=~_-]+)*$!i', $url) ) { echo Matched; } else { echo Did not match; } if ($valido == 0) { something here; } else { something else here; } Thanks a lot in advance, Wagner. -- 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 you must use http://www.aol.com not www.aol.com. coz the later is not a valid url. The protocol is not specified there. and if you use www.aol.comis your href of a tags, the browser will automatically add your current web address as prefix as if its a relative url. if your site is http://www.example.com/folder/site.html and if you use href=/www.aol.com it will show http://www.example.com/ www.aol.com if you use href=www.aol.com it will show http://www.example.com/folder/http://www.aol.com/ www.aol.com you have to use href=http://www.aol.com; the absolute one. -- shout at http://shiplu.awardspace.com/ Available for Hire/Contract/Full Time
Re: [PHP] Regular expression - URL validator
Wagner Garcia Campagner wrote: Thanks Jim, Your sugestion worked perfect for me!! I have another question: After i validate this URL i want to put a link with this URL in my page. The problem is that if the URL is like (www.aol.com), when i create the link, this URL is appended with the URL of my site. The result is a link pointing to: http://mywebsite/www.aol.com But if the URL is like (http://aol.com), then the link is created correct. Is there a way to avoid the first situation... so the link is created correct? Thanks again, Wagner. You could always do a string comparison for http(s)? in the url if ( strpos($url, array('https://', 'http://')) === false ) { $url = 'http://'.$url; } -- 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] Regular expression - URL validator
Hello, I found this regular expression on a web site. It is basicaly an URL validator. I'm trying to implement this in my web site, but i receive errors. I think this is a PERL REGEX so what should i do to make it work in php? $valid = (preg_match('^H|h)(T|t)|(F|f))(T|t)(P|p)((S|s)?))\://)?(www.|[a-zA-Z0-9] .)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,6}(\:[0-9]{1,5})*(/($|[a-zA-Z0-9\.\,\;\?\'\\\ +amp;%\$#\=~_\-]+))*$', $_POST['website'])); if ($valido == 0) { something here; } else { something else here; } Thanks a lot in advance, Wagner. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression - URL validator
Wagner Garcia Campagner wrote: Hello, I found this regular expression on a web site. It is basicaly an URL validator. I'm trying to implement this in my web site, but i receive errors. I think this is a PERL REGEX so what should i do to make it work in php? $valid = (preg_match('^H|h)(T|t)|(F|f))(T|t)(P|p)((S|s)?))\://)?(www.|[a-zA-Z0-9] .)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,6}(\:[0-9]{1,5})*(/($|[a-zA-Z0-9\.\,\;\?\'\\\ +amp;%\$#\=~_\-]+))*$', $_POST['website'])); This should be preg_match('/.../i', $_POST['website']) your regex should look something like this. ^((ftp|(http(s)?))://)?(\.?([a-z0-9-]+))+\.[a-z]{2,6}(:[0-9]{1,5})?(/[a-zA-Z0-9.,;\?|\'+%\$#=~_-]+)*$ So, put it all together and it should look like this. ?php $url = ...PUT YOUR TEST URL HERE...; if ( preg_match('!^((ftp|(http(s)?))://)?(\.?([a-z0-9-]+))+\.[a-z]{2,6}(:[0-9]{1,5})?(/[a-zA-Z0-9.,;\?|\'+%\$#=~_-]+)*$!i', $url) ) { echo Matched; } else { echo Did not match; } if ($valido == 0) { something here; } else { something else here; } Thanks a lot in advance, Wagner. -- 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] Regular Expression just one step away from what I need....
Given the string 'foo''bar''glorp' (all quotes are single quotes)I had hoped to find a regular expression using preg_match that would return an array containing just those words without having to go through additional gyrations, like exploding a string to get an array. I have only had limited luck $theString = 'foo''bar''glorp'; preg_match( /\'(.*)\'/, $theString, $matches); print_r($matches); Array ( [0] = 'foo''bar''glorp' [1] = foo''bar''glorp ) Of course $matches[0] has the entire string and $matches[1] contains the returned string minus the leading and trailing single quote. I can explode $matches[1] to get what I want, but it is an extra step and I am sure that I have done this before but cannot locate the code in question. I would like the results to be Array ( [0] = 'foo''bar''glorp' [1] = foo [2] = bar [3] = glorp ) That way I can use the $matches array without having to create another array to hold the values. I feel as if I am really close on the regex to do this, but cannot seem to find (after much head scratching and teeth gnashing) the proper solution. Much thanks! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression just one step away from what I need....
On Fri, 2007-08-17 at 12:00 -0500, Jay Blanchard wrote: Given the string 'foo''bar''glorp' (all quotes are single quotes)I had hoped to find a regular expression using preg_match that would return an array containing just those words without having to go through additional gyrations, like exploding a string to get an array. I have only had limited luck $theString = 'foo''bar''glorp'; preg_match( /\'(.*)\'/, $theString, $matches); print_r($matches); You want to add the ungreedy modifier... preg_match( /\'(.*)\'/U, $theString, $matches); Cheers, Rob. -- ... SwarmBuy.com - http://www.swarmbuy.com Leveraging the buying power of the masses! ... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression just one step away from what I need....
Jay Blanchard wrote: Given the string 'foo''bar''glorp' (all quotes are single quotes)I had hoped to find a regular expression using preg_match that would return an array containing just those words without having to go through additional gyrations, like exploding a string to get an array. I have only had limited luck $theString = 'foo''bar''glorp'; preg_match( /\'(.*)\'/, $theString, $matches); print_r($matches); Array ( [0] = 'foo''bar''glorp' [1] = foo''bar''glorp ) Of course $matches[0] has the entire string and $matches[1] contains the returned string minus the leading and trailing single quote. I can explode $matches[1] to get what I want, but it is an extra step and I am sure that I have done this before but cannot locate the code in question. I would like the results to be Array ( [0] = 'foo''bar''glorp' [1] = foo [2] = bar [3] = glorp ) That way I can use the $matches array without having to create another array to hold the values. I feel as if I am really close on the regex to do this, but cannot seem to find (after much head scratching and teeth gnashing) the proper solution. Much thanks! If it's only real words this will do: $theString = 'foo''bar''glorp'; preg_match_all( /'([a-z]+)'/Ui, $theString, $matches); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Regular Expression just one step away from what I need....
[snip] I am no regex expert but wouldn't preg_match_all( /'([^']+)'/Ui, $theString, $matches); Be more flexible? [/snip] Thanks all, I completely forgot about greedy/ungreedy. That is what you get for being rusty! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression just one step away from what I need....
I am no regex expert but wouldn't preg_match_all( /'([^']+)'/Ui, $theString, $matches); Be more flexible? On 8/17/07, Thijs Lensselink [EMAIL PROTECTED] wrote: If it's only real words this will do: $theString = 'foo''bar''glorp'; preg_match_all( /'([a-z]+)'/Ui, $theString, $matches); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] regular expression and forbidden words
Hi, i'm french, i'm using regular expressions in php in order to rewrite hyperlink tags in a specific way before apache output is beeing sent to client. Purpose is to replace href attribute of any A html tag by a javascript function calling an ajax loader. Currently I have wrote that: $pattern = '/a href=(http:\/\/dev.netdoor.fr|(\.))\/index.php\?page=(.*?)(.*?)\\/a/i'; $replacement = 'a href=javascript:void(0); OnClick=javascript:yui_fastLoader(\'./systeme/chargeur.php?page=$4\');$5/a'; $html_mark = preg_replace($pattern, $replacement, $html_mark); it works, but it's too permissive, it replaces too much links (some links with some get parameters must stay unchanged) I don't really understand my pattern...it's a little strange for me...lol I wouldn't like to rewrite links that contains any of the following words in get parameters (query string): noLoader noLeftCol skipallbutpage skipservice i would like to rewrite only link that begin with 'http://dev.netdoor.fr' or '.' examples: a href=http://dev.netdoor.fr/index.php?page=./comptes/index.phpnoLoadernoLeftColidx=6bdx=423skipservice;test/a =must be not rewritted! a class=myclass href=./index.php?page=./comptes/mediatheque/index.phpfilter=mov;flv;mpegidx=7name/a =must be rewritted like this : a class=myclass href=javascript:void(0); OnClick=javascript:yui_fastLoader(\'./systeme/chargeur.php?page=./comptes/mediatheque/index.phpfilter=mov;flv;mpegidx=7\');name/a Please help me...:0) best regards nicolas
[PHP] regular expression and forbidden words
Hi, i'm french, i'm using regular expressions in php in order to rewrite hyperlink tags in a specific way before apache output is beeing sent to client. Purpose is to replace href attribute of any A html tag by a javascript function calling an ajax loader. Currently I have wrote that: $pattern = '/a href=(http:\/\/dev.netdoor.fr|(\.))\/index.php\?page=(.*?)(.*?)\\/a/i'; $replacement = 'a href=javascript:void(0); OnClick=javascript:yui_fastLoader(\'./systeme/chargeur.php?page=$4\');$5/a'; $html_mark = preg_replace($pattern, $replacement, $html_mark); it works, but it's too permissive, it replaces too much links (some links with some get parameters must stay unchanged) I don't really understand my pattern...it's a little strange for me...lol I wouldn't like to rewrite links that contains any of the following words in get parameters (query string): noLoader noLeftCol skipallbutpage skipservice i would like to rewrite only link that begin with 'http://dev.netdoor.fr' or '.' examples: a href=http://dev.netdoor.fr/index.php?page=./comptes/index.phpnoLoadernoLeftColidx=6bdx=423skipservice;test/a =must be not rewritted! a class=myclass href=./index.php?page=./comptes/mediatheque/index.phpfilter=mov;flv;mpegidx=7name/a =must be rewritted like this : a class=myclass href=javascript:void(0); OnClick=javascript:yui_fastLoader(\'./systeme/chargeur.php?page=./comptes/mediatheque/index.phpfilter=mov;flv;mpegidx=7\');name/a Please help me...:0) best regards nicolas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression and forbidden words
Nicolas Quirin wrote: Hi, i'm french, i'm using regular expressions in php in order to rewrite hyperlink tags in a specific way before apache output is beeing sent to client. Purpose is to replace href attribute of any A html tag by a javascript function calling an ajax loader. Currently I have wrote that: $pattern = '/a href=(http:\/\/dev.netdoor.fr|(\.))\/index.php\?page=(.*?)(.*?)\\/a/i'; $replacement = 'a href=javascript:void(0); OnClick=javascript:yui_fastLoader(\'./systeme/chargeur.php?page=$4\');$5/a'; $html_mark = preg_replace($pattern, $replacement, $html_mark); it works, but it's too permissive, it replaces too much links (some links with some get parameters must stay unchanged) I don't really understand my pattern...it's a little strange for me...lol I wouldn't like to rewrite links that contains any of the following words in get parameters (query string): noLoader noLeftCol skipallbutpage skipservice i would like to rewrite only link that begin with 'http://dev.netdoor.fr' or '.' examples: a href=http://dev.netdoor.fr/index.php?page=./comptes/index.phpnoLoadernoLeftColidx=6bdx=423skipservice;test/a =must be not rewritted! a class=myclass href=./index.php?page=./comptes/mediatheque/index.phpfilter=mov;flv;mpegidx=7name/a =must be rewritted like this : a class=myclass href=javascript:void(0); OnClick=javascript:yui_fastLoader(\'./systeme/chargeur.php?page=./comptes/mediatheque/index.phpfilter=mov;flv;mpegidx=7\');name/a Please help me...:0) best regards nicolas It's complicated. I'd resort to using a callback. Even if it's possible with just patterns, it wouldn't be very readable! Something like this: if (!function_exists('make_replacement')) { function make_replacement($m) { $n=$m[2]; if (preg_match('~^(http://dev.netdoor.fr|\.)/index\.php\?(.*?)$~',$n,$mm)) { echo MATCHED\n; if (!preg_match('~noLoader|noLeftCol|skipallbutpage|skipservice~',$mm[2])) { echo NOT MATCHED\n; $n='javascript:void(0); '. 'OnClick=javascript:yui_fastLoader(\'./systeme/chargeur.php?'. $mm[2].'\')'; } } return $m[1].'href='.$n.''.$m[3]; } } $pat='~(a.*?\s)href=(.*?)(.*?)~i'; $html_mark=preg_replace_callback($pat,'make_replacement',$html_mark); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression and forbidden words
Nicolas Quirin wrote: Hi, i'm french, i'm using regular expressions in php in order to rewrite hyperlink tags in a specific way before apache output is beeing sent to client. Purpose is to replace href attribute of any A html tag by a javascript function calling an ajax loader. Currently I have wrote that: $pattern = '/a href=(http:\/\/dev.netdoor.fr|(\.))\/index.php\?page=(.*?)(.*?)\\/a/i'; $replacement = 'a href=javascript:void(0); OnClick=javascript:yui_fastLoader(\'./systeme/chargeur.php?page=$4\');$5/a'; $html_mark = preg_replace($pattern, $replacement, $html_mark); it works, but it's too permissive, it replaces too much links (some links with some get parameters must stay unchanged) I don't really understand my pattern...it's a little strange for me...lol I wouldn't like to rewrite links that contains any of the following words in get parameters (query string): noLoader noLeftCol skipallbutpage skipservice i would like to rewrite only link that begin with 'http://dev.netdoor.fr' or '.' examples: a href=http://dev.netdoor.fr/index.php?page=./comptes/index.phpnoLoadernoLeftColidx=6bdx=423skipservice;test/a =must be not rewritted! a class=myclass href=./index.php?page=./comptes/mediatheque/index.phpfilter=mov;flv;mpegidx=7name/a =must be rewritted like this : a class=myclass href=javascript:void(0); OnClick=javascript:yui_fastLoader(\'./systeme/chargeur.php?page=./comptes/mediatheque/index.phpfilter=mov;flv;mpegidx=7\');name/a Please help me...:0) best regards nicolas It's complicated. I'd resort to using a callback. Even if it's possible with just patterns, it wouldn't be very readable! Something like this: if (!function_exists('make_replacement')) { function make_replacement($m) { $n=$m[2]; if (preg_match('~^(http://dev.netdoor.fr|\.)/index\.php\?(.*?)$~', $n,$mm)) { if (!preg_match('~noLoader|noLeftCol|skipallbutpage|skipservice~', $mm[2])) { $n='javascript:void(0); '. 'OnClick=javascript:yui_fastLoader(\'./systeme/chargeur.php?'. $mm[2].'\')'; } } return $m[1].'href='.$n.''.$m[3]; } } $pat='~(a.*?\s)href=(.*?)(.*?)~i'; $html_mark=preg_replace_callback($pat,'make_replacement',$html_mark); Ben. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] regular expression and forbidden words
Hi, i'm french, i'm using regular expressions in php in order to rewrite hyperlink tags in a specific way before apache output is beeing sent to client. Purpose is to replace href attribute of any A html tag by a javascript function calling an ajax loader. Currently I have wrote that: $pattern = '/a href=(http:\/\/dev.netdoor.fr|(\.))\/index.php\?page=(.*?)(.*?)\\/a/i'; $replacement = 'a href=javascript:void(0); OnClick=javascript:yui_fastLoader(\'./systeme/chargeur.php?page=$4\');$5/a'; $html_mark = preg_replace($pattern, $replacement, $html_mark); it works, but it's too permissive, it replaces too much links (some links with some get parameters must stay unchanged) I don't really understand my pattern...it's a little strange for me...lol I wouldn't like to rewrite links that contains any of the following words in get parameters (query string): noLoader noLeftCol skipallbutpage skipservice i would like to rewrite only link that begin with 'http://dev.netdoor.fr' or '.' examples: a href=http://dev.netdoor.fr/index.php?page=./comptes/index.phpnoLoadernoLeftColidx=6bdx=423skipservice;test/a =must be not rewritted! a class=myclass href=./index.php?page=./comptes/mediatheque/index.phpfilter=mov;flv;mpegidx=7name/a =must be rewritted like this : a class=myclass href=javascript:void(0); OnClick=javascript:yui_fastLoader(\'./systeme/chargeur.php?page=./comptes/mediatheque/index.phpfilter=mov;flv;mpegidx=7\');name/a Please help me...:0) best regards nicolas
Re: [PHP] regular expression and forbidden words
Nicolas Quirin wrote: Hi, i'm french, i'm using regular expressions in php in order to rewrite hyperlink tags in a specific way before apache output is beeing sent to client. Purpose is to replace href attribute of any A html tag by a javascript function calling an ajax loader. Currently I have wrote that: $pattern = '/a href=(http:\/\/dev.netdoor.fr|(\.))\/index.php\?page=(.*?)(.*?)\\/a/i'; $replacement = 'a href=javascript:void(0); OnClick=javascript:yui_fastLoader(\'./systeme/chargeur.php?page=$4\');$5/a'; $html_mark = preg_replace($pattern, $replacement, $html_mark); it works, but it's too permissive, it replaces too much links (some links with some get parameters must stay unchanged) I don't really understand my pattern...it's a little strange for me...lol I wouldn't like to rewrite links that contains any of the following words in get parameters (query string): noLoader noLeftCol skipallbutpage skipservice i would like to rewrite only link that begin with 'http://dev.netdoor.fr' or '.' examples: a href=http://dev.netdoor.fr/index.php?page=./comptes/index.phpnoLoadernoLeftColidx=6bdx=423skipservice;test/a =must be not rewritted! a class=myclass href=./index.php?page=./comptes/mediatheque/index.phpfilter=mov;flv;mpegidx=7name/a =must be rewritted like this : a class=myclass href=javascript:void(0); OnClick=javascript:yui_fastLoader(\'./systeme/chargeur.php?page=./comptes/mediatheque/index.phpfilter=mov;flv;mpegidx=7\');name/a Please help me...:0) best regards nicolas The key here is (?!) - the negative assertion, it should look something like this: $p = '#a href=(?:\.|http://dev.netdoor.fr)/index\.php\?page=(?![^]*(?:noLoader|noLeftCol|skipallbutpage|skipservice))([^]*)(.*?)/a#is'; $r = 'a href=javascript:void(0); OnClick=javascript:yui_fastLoader(\'./systeme/chargeur.php?page=$1\');$2/a'; Arpad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular Expression
Do you know good regular expression editor or something simialar? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Fw: [PHP] Regular Expression
- Original Message - From: H.T [EMAIL PROTECTED] To: php-general@lists.php.net Sent: Monday, February 05, 2007 10:12 PM Subject: [PHP] Regular Expression Do you know good regular expression editor or something simialar? This goes into the 'something similar' category: From the documentation: The Regex Coach together with this documentation can be downloaded from http://weitz.de/files/regex-coach.exe (Windows installer) or http://weitz.de/files/regex-coach.tgz (Linux tar archive). It's great to try and test the expressions -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression
On Mon, February 5, 2007 3:12 pm, H.T wrote: Do you know good regular expression editor or something simialar? The Regex Coach Helps you figure things out piece by piece with fancy color highlighting of what matches what, and a tree graph and everything. -- 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] Regular Expression
5 feb 2007 kl. 22.12 skrev H.T: Do you know good regular expression editor or something simialar? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Regex online: www.regextester.com //frank -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular Expression Problem
Hi List I must be dumb as I have been battling my way through regular expression examples for a while and I can not work out why the following does not work properly. I am the first to admit that regular expressions confuse me greatly. The string is a piece of XML as follows and I have put that string into a variable called $xml_string flights flight flightId=19417361 addAdtPrice=0.0 addChdPrice=0.0 addInfPrice=0.0 legXRefs legXRef legId=23862812 class=V/legXRef legXRef legId=23862813 class=V/legXRef /legXRefs /flight flight flightId=19417360 addAdtPrice=0.0 addChdPrice=0.0 addInfPrice=0.0 legXRefs legXRef legId=23862810 class=V/legXRef legXRef legId=23862811 class=V/legXRef /legXRefs /flight /flights What I am trying to do is extract the first flight . /flight chunk. I am using the following, based on the example at http://php.net/manual/en/function.preg-match.php example 3 preg_match('#flight .*\/flight#', $xml_string,$matches); $tempstr = $matches[0]; What I actually get in $tempstr is everything from the first flight through to the last (second) /flight I would have expected preg_match to have matched from the first flight through to the first /flight. I suspect I do not have the regular expression correct. I would great appreciate any help on sorting out the regular expression so that it only returns the first chunk of the string. Regards, Richard Luckhurst -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression Problem
# [EMAIL PROTECTED] / 2007-01-25 09:27:59 +1100: I must be dumb as I have been battling my way through regular expression examples for a while and I can not work out why the following does not work properly. I am the first to admit that regular expressions confuse me greatly. The string is a piece of XML as follows and I have put that string into a variable called $xml_string flights flight flightId=19417361 addAdtPrice=0.0 addChdPrice=0.0 addInfPrice=0.0 legXRefs legXRef legId=23862812 class=V/legXRef legXRef legId=23862813 class=V/legXRef /legXRefs /flight flight flightId=19417360 addAdtPrice=0.0 addChdPrice=0.0 addInfPrice=0.0 legXRefs legXRef legId=23862810 class=V/legXRef legXRef legId=23862811 class=V/legXRef /legXRefs /flight /flights What I am trying to do is extract the first flight . /flight chunk. Are you doing this to learn regular expressions or are you actually trying to do work? Because you're going the wrong way. It's XML, why do you treat it as text? -- How many Vietnam vets does it take to screw in a light bulb? You don't know, man. You don't KNOW. Cause you weren't THERE. http://bash.org/?255991 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re[2]: [PHP] Regular Expression Problem
Hi Roman, RN Are you doing this to learn regular expressions or are you actually RN trying to do work? Because you're going the wrong way. RN It's XML, why do you treat it as text? I am well aware it is XML and I could use an XML parser or simpleXML. I am trying to learn regular expressions as I have a fair bit of work coming up that will require the use pattern matching within strings. It just so happens that some of the data looks like XML, hence the reason I am using a piece of XML for this learning process. Regards Richard -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression Problem
Roman Neuhauser wrote: # [EMAIL PROTECTED] / 2007-01-25 09:27:59 +1100: I must be dumb as I have been battling my way through regular expression examples for a while and I can not work out why the following does not work properly. I am the first to admit that regular expressions confuse me greatly. The string is a piece of XML as follows and I have put that string into a variable called $xml_string flights flight flightId=19417361 addAdtPrice=0.0 addChdPrice=0.0 addInfPrice=0.0 legXRefs legXRef legId=23862812 class=V/legXRef legXRef legId=23862813 class=V/legXRef /legXRefs /flight flight flightId=19417360 addAdtPrice=0.0 addChdPrice=0.0 addInfPrice=0.0 legXRefs legXRef legId=23862810 class=V/legXRef legXRef legId=23862811 class=V/legXRef /legXRefs /flight /flights What I am trying to do is extract the first flight . /flight chunk. accent voice=pirate you be needing an ungreedy modifier on yer regex. /accent see here: http://php.net/manual/en/reference.pcre.pattern.modifiers.php Are you doing this to learn regular expressions or are you actually trying to do work? Because you're going the wrong way. It's XML, why do you treat it as text? not everyone shares that sentiment. in terms of lowest common denominator XML is also a string. then there is the question of whether it's actually marked as XML, whether there is valid DTD and whether the XML itself validates against the DTD - if any of the answers is 'no' then your looking at tag soup. there is a lot more tagsoup in this world than there is proper xml. that said it can't hurt to throw simpleXML at the problem :-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression Problem
# [EMAIL PROTECTED] / 2007-01-24 23:55:27 +0100: Roman Neuhauser wrote: Are you doing this to learn regular expressions or are you actually trying to do work? Because you're going the wrong way. It's XML, why do you treat it as text? not everyone shares that sentiment. in terms of lowest common denominator XML is also a string. then there is the question of whether it's actually marked as XML, whether there is valid DTD and whether the XML itself validates against the DTD - if any of the answers is 'no' then your looking at tag soup. as long as it's wellformed he can use it for what it is: a serialized composite structure, like the result of serialize($aTreeOfObjects). You can use preg_match() to extract a subset of the tree, but why bother if you have unserialize($aTreeOfObjects)-getElementById(foo)? -- How many Vietnam vets does it take to screw in a light bulb? You don't know, man. You don't KNOW. Cause you weren't THERE. http://bash.org/?255991 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression Problem
# [EMAIL PROTECTED] / 2007-01-25 09:27:59 +1100: flights flight flightId=19417361 addAdtPrice=0.0 addChdPrice=0.0 addInfPrice=0.0 legXRefs legXRef legId=23862812 class=V/legXRef legXRef legId=23862813 class=V/legXRef /legXRefs /flight flight flightId=19417360 addAdtPrice=0.0 addChdPrice=0.0 addInfPrice=0.0 legXRefs legXRef legId=23862810 class=V/legXRef legXRef legId=23862811 class=V/legXRef /legXRefs /flight /flights What I am trying to do is extract the first flight . /flight chunk. preg_match('#flight .*\/flight#', $xml_string,$matches); $tempstr = $matches[0]; What I actually get in $tempstr is everything from the first flight through to the last (second) /flight Strange, you should have gotten nothing: s (PCRE_DOTALL) If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. I would have expected preg_match to have matched from the first flight through to the first /flight. As Jochem wrote, you also need U (PCRE_UNGREEDY). -- How many Vietnam vets does it take to screw in a light bulb? You don't know, man. You don't KNOW. Cause you weren't THERE. http://bash.org/?255991 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re[2]: [PHP] Regular Expression Problem
Hi Jochem, JM accent voice=pirate JM you be needing an ungreedy modifier on yer regex. JM /accent JM see here: JM http://php.net/manual/en/reference.pcre.pattern.modifiers.php RL Thanks very much. That solved my problem and I my now getting exactly what I RL want. I had seen the reference to greedy and ungreedy but had not understood the RL concept. RL Regards RL Richard -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression Problem
At 1/24/2007 02:27 PM, Richard Luckhurst wrote: What I am trying to do is extract the first flight . /flight chunk. ... preg_match('#flight .*\/flight#', $xml_string,$matches); $tempstr = $matches[0]; What I actually get in $tempstr is everything from the first flight through to the last (second) /flight I would have expected preg_match to have matched from the first flight through to the first /flight. At 1/24/2007 02:55 PM, Jochem Maas wrote: accent voice=pirate you be needing an ungreedy modifier on yer regex. /accent see here: http://php.net/manual/en/reference.pcre.pattern.modifiers.php You can also ask regexp to return the text up to but not including the next '': #flight [^]+# 'flight ' followed by one or more characters that aren't ''. Regards, Paul __ Juniper Webcraft Ltd. http://juniperwebcraft.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression Problem
You want to add a 'U' after you closing # so that matches are Ungreedy -- I.e., they do NOT grab as much text as the can to fulfill the pattern (greedy) but they grab as LITTLE text as they can to fulfill the pattern (ungreedy) On Wed, January 24, 2007 4:27 pm, Richard Luckhurst wrote: Hi List I must be dumb as I have been battling my way through regular expression examples for a while and I can not work out why the following does not work properly. I am the first to admit that regular expressions confuse me greatly. The string is a piece of XML as follows and I have put that string into a variable called $xml_string flights flight flightId=19417361 addAdtPrice=0.0 addChdPrice=0.0 addInfPrice=0.0 legXRefs legXRef legId=23862812 class=V/legXRef legXRef legId=23862813 class=V/legXRef /legXRefs /flight flight flightId=19417360 addAdtPrice=0.0 addChdPrice=0.0 addInfPrice=0.0 legXRefs legXRef legId=23862810 class=V/legXRef legXRef legId=23862811 class=V/legXRef /legXRefs /flight /flights What I am trying to do is extract the first flight . /flight chunk. I am using the following, based on the example at http://php.net/manual/en/function.preg-match.php example 3 preg_match('#flight .*\/flight#', $xml_string,$matches); $tempstr = $matches[0]; What I actually get in $tempstr is everything from the first flight through to the last (second) /flight I would have expected preg_match to have matched from the first flight through to the first /flight. I suspect I do not have the regular expression correct. I would great appreciate any help on sorting out the regular expression so that it only returns the first chunk of the string. Regards, Richard Luckhurst -- 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 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] Regular Expression Problem
Richard Luckhurst wrote: Hi Jochem, JM accent voice=pirate JM you be needing an ungreedy modifier on yer regex. JM /accent JM see here: JM http://php.net/manual/en/reference.pcre.pattern.modifiers.php Thanks very much. That solved my problem and I my now getting exactly what I want. I had seen the reference to greedy and ungreedy but had not understood the concept. for a better understanding I recommend the film WallStreet - that should get you up to speed on the concept of greed ;-) Regards Richard -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression Problem
Roman Neuhauser wrote: # [EMAIL PROTECTED] / 2007-01-24 23:55:27 +0100: Roman Neuhauser wrote: Are you doing this to learn regular expressions or are you actually trying to do work? Because you're going the wrong way. It's XML, why do you treat it as text? not everyone shares that sentiment. in terms of lowest common denominator XML is also a string. then there is the question of whether it's actually marked as XML, whether there is valid DTD and whether the XML itself validates against the DTD - if any of the answers is 'no' then your looking at tag soup. as long as it's wellformed he can use it for what it is: a serialized composite structure, like the result of serialize($aTreeOfObjects). You can use preg_match() to extract a subset of the tree, but why bother if you have unserialize($aTreeOfObjects)-getElementById(foo)? nothing to do with this argument but: this reminds of a thread on internals where someone was trying to determine if a serialized data was *safe* to run ... the data was coming from a cookie (I think), unserializing the data without first checking it didn't contain something evil was a bad idea - the proposed solution was a fairly simple regex that checked for 'object' notation in the serialized string. :-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] regular expression help!
Hello, Can someone here give me a glue how to do the following. I guess I need to use regular expressions here. I have absolutely zero experience with regular expressions. (if there's another way to do this I don't mind. I jus need to get this done somehow :) I need to strip all characters from the following text string exept the image path... img width=\99\ height=\120\ border=\0\ src=\../../images/new/thumps/4123141112007590373240.jpg\ /...and then store the path to DB. Image path lengh can vary so I guess that I need to extract all characters after scr=\until next\or somethig similar. Thanks for your advise! -Will -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression help!
# [EMAIL PROTECTED] / 2007-01-18 12:34:36 +0200: I need to strip all characters from the following text string exept the image path... img width=\99\ height=\120\ border=\0\ src=\../../images/new/thumps/4123141112007590373240.jpg\ /...and then store the path to DB. Image path lengh can vary so I guess that I need to extract all characters after scr=\until next\or somethig similar. This passes with 5.2: class ImgSrcTest extends Tence_TestCase { private $src, $str, $xml; function setUp() { $this-src = 'fubar.jpg'; $this-str = sprintf( 'img width=99 height=120 border=0 src=%s /', $this-src ); $this-xml = new SimpleXmlElement($this-str); } function testReturnsAttributeAsSimpleXMLElements() { return $this-assertEquals('SimpleXMLElement', get_class($this-xml['src'])); } function testCastToStringYieldsTheAttributeValue() { return $this-assertEquals($this-src, strval($this-xml['src'])); } } -- How many Vietnam vets does it take to screw in a light bulb? You don't know, man. You don't KNOW. Cause you weren't THERE. http://bash.org/?255991 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression help!
Hello Roman, Could you specify the functionality of your script a bit please. (How it works) I forgot to mention that this part: img width=99 height=120 border=0 src=%s /', is not always the same. The image properties can vary. Thanks -Will Roman Neuhauser [EMAIL PROTECTED] kirjoitti viestissä:[EMAIL PROTECTED] # [EMAIL PROTECTED] / 2007-01-18 12:34:36 +0200: I need to strip all characters from the following text string exept the image path... img width=\99\ height=\120\ border=\0\ src=\../../images/new/thumps/4123141112007590373240.jpg\ /...and then store the path to DB. Image path lengh can vary so I guess that I need to extract all characters after scr=\until next\or somethig similar. This passes with 5.2: class ImgSrcTest extends Tence_TestCase { private $src, $str, $xml; function setUp() { $this-src = 'fubar.jpg'; $this-str = sprintf( 'img width=99 height=120 border=0 src=%s /', $this-src ); $this-xml = new SimpleXmlElement($this-str); } function testReturnsAttributeAsSimpleXMLElements() { return $this-assertEquals('SimpleXMLElement', get_class($this-xml['src'])); } function testCastToStringYieldsTheAttributeValue() { return $this-assertEquals($this-src, strval($this-xml['src'])); } } -- How many Vietnam vets does it take to screw in a light bulb? You don't know, man. You don't KNOW. Cause you weren't THERE. http://bash.org/?255991 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression help!
William Stokes wrote: Hello Roman, Could you specify the functionality of your script a bit please. (How it works) it's a hint as to how you might use simpleXML to extract the values of a src attribute from the definition of an img tag. I forgot to mention that this part: img width=99 height=120 border=0 src=%s /', is not always the same. The image properties can vary. Thanks -Will Roman Neuhauser [EMAIL PROTECTED] kirjoitti viestissä:[EMAIL PROTECTED] # [EMAIL PROTECTED] / 2007-01-18 12:34:36 +0200: I need to strip all characters from the following text string exept the image path... img width=\99\ height=\120\ border=\0\ src=\../../images/new/thumps/4123141112007590373240.jpg\ /...and then store the path to DB. Image path lengh can vary so I guess that I need to extract all characters after scr=\until next\or somethig similar. This passes with 5.2: class ImgSrcTest extends Tence_TestCase { private $src, $str, $xml; function setUp() { $this-src = 'fubar.jpg'; $this-str = sprintf( 'img width=99 height=120 border=0 src=%s /', $this-src ); $this-xml = new SimpleXmlElement($this-str); } function testReturnsAttributeAsSimpleXMLElements() { return $this-assertEquals('SimpleXMLElement', get_class($this-xml['src'])); } function testCastToStringYieldsTheAttributeValue() { return $this-assertEquals($this-src, strval($this-xml['src'])); } } -- How many Vietnam vets does it take to screw in a light bulb? You don't know, man. You don't KNOW. Cause you weren't THERE. http://bash.org/?255991 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression help!
(Haven't received William's email yet = scavenging Jochem's reply.) # [EMAIL PROTECTED] / 2007-01-18 18:01:19 +0100: William Stokes wrote: Roman Neuhauser [EMAIL PROTECTED] kirjoitti This passes with 5.2: class ImgSrcTest extends Tence_TestCase { private $src, $str, $xml; function setUp() { $this-src = 'fubar.jpg'; $this-str = sprintf( 'img width=99 height=120 border=0 src=%s /', $this-src ); $this-xml = new SimpleXmlElement($this-str); } function testReturnsAttributeAsSimpleXMLElements() { return $this-assertEquals( 'SimpleXMLElement', get_class($this-xml['src']) ); } function testCastToStringYieldsTheAttributeValue() { return $this-assertEquals($this-src, strval($this-xml['src'])); } } Could you specify the functionality of your script a bit please. (How it works) It's a unit test showing how you can use SimpleXML to do what you want. It's written for Testilence out of sheer laziness, but any unit testing tool would do (after cosmetic changes to the code). The test case contains two pretty obvious tests, just read the function names, their bodies should be pretty obvious. On thing that might be a little non-obvious is the strval(): that was the only way I found to get the string out of the very uncommunicative SimpleXMLElement. I forgot to mention that this part: img width=99 height=120 border=0 src=%s /', is not always the same. The image properties can vary. That's one of the reasons I didn't go the regexp route. Don't treat XML as text, it's not. -- How many Vietnam vets does it take to screw in a light bulb? You don't know, man. You don't KNOW. Cause you weren't THERE. http://bash.org/?255991 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular Expression help
?php echo 'Is String: [' . ( is_string( 'a1b2c3' ) preg_match( '/[A-Za-z]+/', 'a1b2c3' )) . ']br'; echo 'Is Numeric: [' . ( is_numeric( 'a1b2c3' ) preg_match( '/[0-9]+/', 'a1b2c3' )) . ']br'; echo 'Is String: [' . ( is_string( 'abcdef' ) preg_match( '/[A-Za-z]+/', 'abcdef' )) . ']br'; echo 'Is Numeric: [' . ( is_numeric( '123456' ) preg_match( '/[0-9]+/', '123456' )) . ']br'; ? Why is the first Is String check returning true/showing 1? preg_match should fail because 'a1b2c3' contains numbers and, as such, doesn't match the pattern... What am I doing wrong? thnx, Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression help
Those patterns aren't anchored to the ends of the string, so as long as the string contains one matching character, the succeeds. ^ anchors the pattern to the beginning, \z to the end, so you want: /^[A-Za-z]+\z/ Or test the opposite case to see if it fails: /[^A-Za-z]/ Arpad Chris Boget wrote: ?php echo 'Is String: [' . ( is_string( 'a1b2c3' ) preg_match( '/[A-Za-z]+/', 'a1b2c3' )) . ']br'; echo 'Is Numeric: [' . ( is_numeric( 'a1b2c3' ) preg_match( '/[0-9]+/', 'a1b2c3' )) . ']br'; echo 'Is String: [' . ( is_string( 'abcdef' ) preg_match( '/[A-Za-z]+/', 'abcdef' )) . ']br'; echo 'Is Numeric: [' . ( is_numeric( '123456' ) preg_match( '/[0-9]+/', '123456' )) . ']br'; ? Why is the first Is String check returning true/showing 1? preg_match should fail because 'a1b2c3' contains numbers and, as such, doesn't match the pattern... What am I doing wrong? thnx, Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression help
Chris Boget wrote: ?php echo 'Is String: [' . ( is_string( 'a1b2c3' ) preg_match( '/[A-Za-z]+/', 'a1b2c3' )) . ']br'; echo 'Is Numeric: [' . ( is_numeric( 'a1b2c3' ) preg_match( '/[0-9]+/', 'a1b2c3' )) . ']br'; echo 'Is String: [' . ( is_string( 'abcdef' ) preg_match( '/[A-Za-z]+/', 'abcdef' )) . ']br'; echo 'Is Numeric: [' . ( is_numeric( '123456' ) preg_match( '/[0-9]+/', '123456' )) . ']br'; ? Why is the first Is String check returning true/showing 1? preg_match should fail because 'a1b2c3' contains numbers and, as such, doesn't match the pattern... It does match the pattern. The expression says 1 or more A-Za-z in sequence. If you want to check against the whole string you need to add the start and end markers... preg_match( '/^[A-Za-z]+$/', 'a1b2c3' )) Look at the manual page for preg_match and read up on the third parameter. Use it to see what is matching the expression. -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression help
Note that $ allows a trailing newline, but \z doesn't. Arpad Stut wrote: Chris Boget wrote: ?php echo 'Is String: [' . ( is_string( 'a1b2c3' ) preg_match( '/[A-Za-z]+/', 'a1b2c3' )) . ']br'; echo 'Is Numeric: [' . ( is_numeric( 'a1b2c3' ) preg_match( '/[0-9]+/', 'a1b2c3' )) . ']br'; echo 'Is String: [' . ( is_string( 'abcdef' ) preg_match( '/[A-Za-z]+/', 'abcdef' )) . ']br'; echo 'Is Numeric: [' . ( is_numeric( '123456' ) preg_match( '/[0-9]+/', '123456' )) . ']br'; ? Why is the first Is String check returning true/showing 1? preg_match should fail because 'a1b2c3' contains numbers and, as such, doesn't match the pattern... It does match the pattern. The expression says 1 or more A-Za-z in sequence. If you want to check against the whole string you need to add the start and end markers... preg_match( '/^[A-Za-z]+$/', 'a1b2c3' )) Look at the manual page for preg_match and read up on the third parameter. Use it to see what is matching the expression. -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php