Re: [PHP] preg_match() occurence position
On Thursday 25 July 2002 20:40, lallous wrote: Hello, Can I get the starting position of my string occurence when using any regexps searching functions in PHP ? for example: $mem='this is a test'; preg_match('/test/', ...) should return me somehow: 10 No. If search string does not involve a regex then just use the plain string search functions. -- Jason Wong - Gremlins Associates - www.gremlins.com.hk Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * /* We will have solar energy as soon as the utility companies solve one technical problem -- how to run a sunbeam through a meter. */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_match() occurence position
Hello lallous, Thursday, July 25, 2002, 3:40:32 PM, you wrote: l Hello, l Can I get the starting position of my string occurence when using any l regexps searching functions in PHP ? l for example: l $mem='this is a test'; l preg_match('/test/', ...) should return me somehow: 10 l Thanks why regexps? what about simple string fuctions in PHP? for your example they will be more usefull: strrpos - http://www.php.net/manual/en/function.strrpos.php sample: $text = string; echo strrpos($text, r); // must show 2 as result -- Best regards, Alexander Kuznetsov -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] preg_match() occurence position
Can I get the starting position of my string occurence when using any regexps searching functions in PHP ? for example: $mem='this is a test'; preg_match('/test/', ...) should return me somehow: 10 Does it have to be a regular expression you are searching for? If not, you can use strpos(). www.php.net/strpos ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_match() occurence position
Yes, I'm aware of the strpos() or any other non-regexp string functions... but it is either I use regexp to match a certain pattern or I'll have to write a char-by-char parser to emulate regexp searching and yet get the position of the occurence! Thank you all anyway. //Elias Alexander Kuznetsov [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Hello lallous, Thursday, July 25, 2002, 3:40:32 PM, you wrote: l Hello, l Can I get the starting position of my string occurence when using any l regexps searching functions in PHP ? l for example: l $mem='this is a test'; l preg_match('/test/', ...) should return me somehow: 10 l Thanks why regexps? what about simple string fuctions in PHP? for your example they will be more usefull: strrpos - http://www.php.net/manual/en/function.strrpos.php sample: $text = string; echo strrpos($text, r); // must show 2 as result -- Best regards, Alexander Kuznetsov -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_match() occurence position
On Thu, 25 Jul 2002 15:05:36 +0200, you wrote: l for example: l $mem='this is a test'; l preg_match('/test/', ...) should return me somehow: 10 How about this: $mem='this is a test'; if(preg_match(/(.*)test/,$mem,$matches)) { echo strlen($matches[1]); } Of course, if test occurs more than once then the above won't work, since regex's are greedy... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_match() occurence position
On Thursday, July 25, 2002, Michael Sims wrote: On Thu, 25 Jul 2002 15:05:36 +0200, you wrote: for example: $mem='this is a test'; preg_match('/test/', ...) should return me somehow: 10 How about this: $mem='this is a test'; if(preg_match(/(.*)test/,$mem,$matches)) { echo strlen($matches[1]); } Of course, if test occurs more than once then the above won't work, since regex's are greedy... If you add a ? after the * then that'll make it non-greedy won't it? -- Tim Fountain ([EMAIL PROTECTED]) http://www.tfountain.co.uk/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_match() occurence position
on 25/07/02 11:05 PM, lallous ([EMAIL PROTECTED]) wrote: Yes, I'm aware of the strpos() or any other non-regexp string functions... but it is either I use regexp to match a certain pattern or I'll have to write a char-by-char parser to emulate regexp searching and yet get the position of the occurence! Actually, I've just written one of them, and they're not that hard, as long as the sub-set of what you want to do is limited. Justin French -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_match() occurence position
Yes, I've done it before too...but I saw some other libraries(in other programming langs) that has such features... Justin French [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... on 25/07/02 11:05 PM, lallous ([EMAIL PROTECTED]) wrote: Yes, I'm aware of the strpos() or any other non-regexp string functions... but it is either I use regexp to match a certain pattern or I'll have to write a char-by-char parser to emulate regexp searching and yet get the position of the occurence! Actually, I've just written one of them, and they're not that hard, as long as the sub-set of what you want to do is limited. Justin French -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_match() occurence position
How about this: $text = this is test; preg_match can return the first match in an optional array I'll call $matches http://www.php.net/manual/en/function.preg-match.php preg_match('/test/', $text, $matches); strpos returns the numeric position of the first occurrence http://www.php.net/manual/en/function.strpos.php $pos = strpos($text, $matches[0]); die($pos); // should be what you want... 10 Jim Grill Support Web-1 Hosting http://www.web-1hosting.net - Original Message - From: lallous [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, July 25, 2002 7:40 AM Subject: [PHP] preg_match() occurence position Hello, Can I get the starting position of my string occurence when using any regexps searching functions in PHP ? for example: $mem='this is a test'; preg_match('/test/', ...) should return me somehow: 10 Thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_match() occurence position
On Thu, 25 Jul 2002, lallous wrote: Yes, I'm aware of the strpos() or any other non-regexp string functions... but it is either I use regexp to match a certain pattern or I'll have to write a char-by-char parser to emulate regexp searching and yet get the position of the occurence! Once you have found the matching string with preg_match, you can use strpos() to see where it was. miguel -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php