Re: [PHP-DEV] strpos() suggestion
Hi, from 4.3.0 it will be ok to use strcspn(), atm (int 4.2.3) it has 2 params. From 4.3.0 it has up to four params. It the second pair is like in substr(). Start index can be given, or even start index and how much chars to be checked. The same behavior is true and for strspn() (starting 4.3.0) but for your case strcspn() is the function. Andrey - Original Message - From: Monte Ohrt [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, November 14, 2002 12:02 AM Subject: [PHP-DEV] strpos() suggestion Hi, I had a little problem to solve today, and couldn't find any easy way to do it without some extra steps slicing things up. What I wanted to do is take an arbitrary point in a string, and find the position of the first '[' on the left of it, and the first ']' on the right of it. Finding the ']' was easy, I use strpos() to find the first occurance of ']' after my given point. Finding '[' however is not so easy. strrpos() was my first guess, but it does not work like strpos() at all. There is no optional third parameter, and on an unrelated note it only works with a single character, not a string. inconsistant ;-) My best solution was to slice the string at my point, then get the position of '[' with strrpos(). I'm not sure of the most intuitive way to solve this. One way would be to add a feature to strpos(); if you supply a negative third parameter, it gives you the position of the first occurance to the _left_ of that point. I'm not sure if the number should represent the position from the end or beginning of the string. Another way, add a third parameter to strrpos() to start at a given point from the end of the string. I'd try submitting a patch, but I'm not sure of which way would be best, and my C is a bit rusty, I'd probably do more damage than help ;-) Thoughts? Monte -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-DEV] strpos() suggestion
Hi, I had a little problem to solve today, and couldn't find any easy way to do it without some extra steps slicing things up. What I wanted to do is take an arbitrary point in a string, and find the position of the first '[' on the left of it, and the first ']' on the right of it. Finding the ']' was easy, I use strpos() to find the first occurance of ']' after my given point. Finding '[' however is not so easy. strrpos() was my first guess, but it does not work like strpos() at all. There is no optional third parameter, and on an unrelated note it only works with a single character, not a string. inconsistant ;-) My best solution was to slice the string at my point, then get the position of '[' with strrpos(). I'm not sure of the most intuitive way to solve this. One way would be to add a feature to strpos(); if you supply a negative third parameter, it gives you the position of the first occurance to the _left_ of that point. I'm not sure if the number should represent the position from the end or beginning of the string. Another way, add a third parameter to strrpos() to start at a given point from the end of the string. I'd try submitting a patch, but I'm not sure of which way would be best, and my C is a bit rusty, I'd probably do more damage than help ;-) Thoughts? Monte -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] strpos() suggestion
The reason the functions are the way they are is because they directly mirror the underlying C functions. strrpos() calls strrchr() directly. But yes, some sort of new php_memnstr() based string searcher could probably be written. -Rasmus On 13 Nov 2002, Monte Ohrt wrote: Hi, I had a little problem to solve today, and couldn't find any easy way to do it without some extra steps slicing things up. What I wanted to do is take an arbitrary point in a string, and find the position of the first '[' on the left of it, and the first ']' on the right of it. Finding the ']' was easy, I use strpos() to find the first occurance of ']' after my given point. Finding '[' however is not so easy. strrpos() was my first guess, but it does not work like strpos() at all. There is no optional third parameter, and on an unrelated note it only works with a single character, not a string. inconsistant ;-) My best solution was to slice the string at my point, then get the position of '[' with strrpos(). I'm not sure of the most intuitive way to solve this. One way would be to add a feature to strpos(); if you supply a negative third parameter, it gives you the position of the first occurance to the _left_ of that point. I'm not sure if the number should represent the position from the end or beginning of the string. Another way, add a third parameter to strrpos() to start at a given point from the end of the string. I'd try submitting a patch, but I'm not sure of which way would be best, and my C is a bit rusty, I'd probably do more damage than help ;-) Thoughts? Monte -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] strpos() suggestion
Why couldn't you just strrev() the string and then strpos() for it? Mike --- Original Message --- From:Monte Ohrt [EMAIL PROTECTED] To: [EMAIL PROTECTED] Date:13 Nov 2002 16:02:54 -0600 Subject: [PHP-DEV] strpos() suggestion Hi, I had a little problem to solve today, and couldn't find any easy way to do it without some extra steps slicing things up. What I wanted to do is take an arbitrary point in a string, and find the position of the first '[' on the left of it, and the first ']' on the right of it. Finding the ']' was easy, I use strpos() to find the first occurance of ']' after my given point. Finding '[' however is not so easy. strrpos() was my first guess, but it does not work like strpos() at all. There is no optional third parameter, and on an unrelated note it only works with a single character, not a string. inconsistant ;-) My best solution was to slice the string at my point, then get the position of '[' with strrpos(). I'm not sure of the most intuitive way to solve this. One way would be to add a feature to strpos(); if you supply a negative third parameter, it gives you the position of the first occurance to the _left_ of that point. I'm not sure if the number should represent the position from the end or beginning of the string. Another way, add a third parameter to strrpos() to start at a given point from the end of the string. I'd try submitting a patch, but I'm not sure of which way would be best, and my C is a bit rusty, I'd probably do more damage than help ;-) Thoughts? Monte -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] strpos() suggestion
There are many ways to do it, but everything requires a manipulation to the string. strrev() would not only require the entire string to be swapped around, but you'd also have to recompute the string position to count from. The best way as it stands (that I see) is to slice the array then use strrpos(), as stated below. But I'm suggesting a feature to be able to do this kind of calculation the quickest, ie. no extra vars to create, no manipulation to the string required. On Wed, 2002-11-13 at 16:24, Mike Hall wrote: Why couldn't you just strrev() the string and then strpos() for it? Mike --- Original Message --- From:Monte Ohrt [EMAIL PROTECTED] To: [EMAIL PROTECTED] Date:13 Nov 2002 16:02:54 -0600 Subject: [PHP-DEV] strpos() suggestion Hi, I had a little problem to solve today, and couldn't find any easy way to do it without some extra steps slicing things up. What I wanted to do is take an arbitrary point in a string, and find the position of the first '[' on the left of it, and the first ']' on the right of it. Finding the ']' was easy, I use strpos() to find the first occurance of ']' after my given point. Finding '[' however is not so easy. strrpos() was my first guess, but it does not work like strpos() at all. There is no optional third parameter, and on an unrelated note it only works with a single character, not a string. inconsistant ;-) My best solution was to slice the string at my point, then get the position of '[' with strrpos(). I'm not sure of the most intuitive way to solve this. One way would be to add a feature to strpos(); if you supply a negative third parameter, it gives you the position of the first occurance to the _left_ of that point. I'm not sure if the number should represent the position from the end or beginning of the string. Another way, add a third parameter to strrpos() to start at a given point from the end of the string. I'd try submitting a patch, but I'm not sure of which way would be best, and my C is a bit rusty, I'd probably do more damage than help ;-) Thoughts? Monte -- Monte Ohrt [EMAIL PROTECTED] -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php