strpos - and multiple needles?
strpos - and multiple needles?
am 28.12.2007 14:34:54 von jodleren
Hi!
I was reading php.net for a way to find a number of characters in a
strings. Say I want to look for a b and c ind $string.
As far as I could see, there is no function for that.
So, I can either?
1) run through the string and test using in_array('a', 'b', 'c')
2) run through array and test using strpos( $items[$i]....
Where I find #2 the best.
But is there a function which can do that faster or in a better way?
Re: strpos - and multiple needles?
am 28.12.2007 15:09:35 von ivansanchez-alg
jodleren wrote:
> Hi!
>
> I was reading php.net for a way to find a number of characters in a
> strings. Say I want to look for a b and c ind $string.
> As far as I could see, there is no function for that.
preg_match("[abc]",$subject);
You might find reading something on regular expressions useful.
Cheers,
--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-
Proudly running Debian Linux with 2.6.22-3-amd64 kernel, KDE 3.5.8, and PHP
5.2.4-2 generating this signature.
Uptime: 14:37:42 up 36 days, 53 min, 4 users, load average: 0.40, 0.81,
0.94
Re: strpos - and multiple needles?
am 28.12.2007 15:13:54 von My Pet Programmer
jodleren said:
> Hi!
>
> I was reading php.net for a way to find a number of characters in a
> strings. Say I want to look for a b and c ind $string.
> As far as I could see, there is no function for that.
>
> So, I can either?
> 1) run through the string and test using in_array('a', 'b', 'c')
> 2) run through array and test using strpos( $items[$i]....
> Where I find #2 the best.
>
> But is there a function which can do that faster or in a better way?
So you have one string, and you want to see if each of your elements are
in it individually.
So:
$needles = array("a", "b", "c");
$haystack = "This is a string with a certain number of each character in
it.";
$counts = array();
function countChars($letter, $key, $arr) {
global $output;
$garbage = explode($letter, $arr[0]);
$arr[1][$letter] = count($garbage)-1;
}
array_walk($needles, 'countChars', array($haystack, &$counts));
print_r($counts); // Array ( [a] => 6 [b] => 1 [c] => 4 )
That's a little cleaner, I suppose. You're still going through a bunch,
but if you're just counting, that will return an array that looks like that.
All the best,
~A!
--
Anthony Levensalor
anthony@mypetprogrammer.com
Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Re: strpos - and multiple needles?
am 28.12.2007 15:16:32 von My Pet Programmer
Iv=E1n S=E1nchez Ortega said:
> jodleren wrote:
[snip]
> preg_match("[abc]",$subject);
>=20
Oh, see, that's much better than what I did.
>=20
> You might find reading something on regular expressions useful.
And so, it seems, would I. Thanks!
~A!
--=20
Anthony Levensalor
anthony@mypetprogrammer.com
Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Re: strpos - and multiple needles?
am 28.12.2007 15:38:20 von My Pet Programmer
Iv=E1n S=E1nchez Ortega said:
> jodleren wrote:
>=20
>> Hi!
>>
>> I was reading php.net for a way to find a number of characters in a
>> strings. Say I want to look for a b and c ind $string.
>> As far as I could see, there is no function for that.
>=20
> preg_match("[abc]",$subject);
>=20
>=20
> You might find reading something on regular expressions useful.
>=20
>=20
> Cheers,
That will only tell you if any of the characters are in the string. Do=20
you have one that will say whether all of them are in the haystack?
--=20
Anthony Levensalor
anthony@mypetprogrammer.com
Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Re: strpos - and multiple needles?
am 28.12.2007 15:43:16 von ivansanchez-alg
My Pet Programmer wrote:
>>> I was reading php.net for a way to find a number of characters in a
>>> strings. Say I want to look for a b and c ind $string.
>>> As far as I could see, there is no function for that.
>>
>> preg_match("[abc]",$subject);
>
> That will only tell you if any of the characters are in the string. Do
> you have one that will say whether all of them are in the haystack?
Are you looking for http://php.net/count_chars ??
What are you trying to do, exactly?
--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-
MSN:i_eat_s_p_a_m_for_breakfast@hotmail.com
Jabber:ivansanchez@jabber.org ; ivansanchez@kdetalk.net
Re: strpos - and multiple needles?
am 28.12.2007 15:51:42 von My Pet Programmer
Iv=E1n S=E1nchez Ortega said:
> My Pet Programmer wrote:
>=20
>>>> I was reading php.net for a way to find a number of characters in a
>>>> strings. Say I want to look for a b and c ind $string.
>>>> As far as I could see, there is no function for that.
>>> preg_match("[abc]",$subject);
>> That will only tell you if any of the characters are in the string. Do=
>> you have one that will say whether all of them are in the haystack?
>=20
> Are you looking for http://php.net/count_chars ??
>=20
> What are you trying to do, exactly?
>=20
Oh, for me it was just idle curiosity. I'm not sure what the OP is after.=
~A!
--=20
Anthony Levensalor
anthony@mypetprogrammer.com
Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Re: strpos - and multiple needles?
am 28.12.2007 16:03:08 von jodleren
On Dec 28, 4:43=A0pm, Iv=E1n S=E1nchez Ortega
escomposlinux.-.punto.-.org> wrote:
> My Pet Programmer wrote:
> >>> I was reading php.net for a way to find a number of characters in a
> >>> strings. Say I want to look for a b and c ind $string.
> >>> As far as I could see, there is no function for that.
> >> preg_match("[abc]",$subject);
> > That will only tell you if any of the characters are in the string. Do
> > you have one that will say whether all of them are in the haystack?
> Are you looking forhttp://php.net/count_chars??
> What are you trying to do, exactly?
To check for invalid characters in a filename
WBR
Sonnich
Re: strpos - and multiple needles?
am 28.12.2007 16:11:19 von My Pet Programmer
jodleren said:
> On Dec 28, 4:43 pm, Iv=E1n S=E1nchez Ortega
> escomposlinux.-.punto.-.org> wrote:
>> My Pet Programmer wrote:
>>>>> I was reading php.net for a way to find a number of characters in a=
>>>>> strings. Say I want to look for a b and c ind $string.
>>>>> As far as I could see, there is no function for that.
>>>> preg_match("[abc]",$subject);
>>> That will only tell you if any of the characters are in the string. D=
o
>>> you have one that will say whether all of them are in the haystack?
>> Are you looking forhttp://php.net/count_chars??
>> What are you trying to do, exactly?
>=20
> To check for invalid characters in a filename
>=20
> WBR
> Sonnich
preg_match("*[abc]*", $haystack); worked for me.
~A!
--=20
Anthony Levensalor
anthony@mypetprogrammer.com
Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Re: strpos - and multiple needles?
am 28.12.2007 16:20:45 von ivansanchez-alg
jodleren wrote:
> To check for invalid characters in a filename
Escape the filename, then.
--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-
MSN:i_eat_s_p_a_m_for_breakfast@hotmail.com
Jabber:ivansanchez@jabber.org ; ivansanchez@kdetalk.net
Re: strpos - and multiple needles?
am 28.12.2007 16:33:17 von jodleren
On Dec 28, 5:20=A0pm, Iv=E1n S=E1nchez Ortega
escomposlinux.-.punto.-.org> wrote:
> jodleren wrote:
> > To check for invalid characters in a filename
> Escape the filename, then.
Sorry, I dont get this one.
Re: strpos - and multiple needles?
am 28.12.2007 16:41:10 von ivansanchez-alg
jodleren wrote:
> On Dec 28, 5:20 pm, Iván Sánchez Ortega
> escomposlinux.-.punto.-.org> wrote:
>> jodleren wrote:
>> > To check for invalid characters in a filename
>> Escape the filename, then.
>
> Sorry, I dont get this one.
OK... why do you need to check for invalid characters in a filename?
In other words, where could a filename with "strange" characters cause
damage to your system?
--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-
La costumbre es una segunda naturaleza.- Galeno.
Re: strpos - and multiple needles?
am 28.12.2007 17:09:02 von jodleren
On Dec 28, 5:41=A0pm, Iv=E1n S=E1nchez Ortega
escomposlinux.-.punto.-.org> wrote:
> jodleren wrote:
> > On Dec 28, 5:20=A0pm, Iv=E1n S=E1nchez Ortega
> > escomposlinux.-.punto.-.org> wrote:
> >> jodleren wrote:
> >> > To check for invalid characters in a filename
> >> Escape the filename, then.
>
> > Sorry, I dont get this one.
>
> OK... why do you need to check for invalid characters in a filename?
>
> In other words, where could a filename with "strange" characters cause
> damage to your system?
Windows based systems do not allow certain characters. I need to check
for them, _even_ that my server can handle them, I still have to think
of the user downloading a file etc....
Re: strpos - and multiple needles?
am 28.12.2007 17:17:57 von ivansanchez-alg
jodleren wrote:
> Windows based systems do not allow certain characters. I need to check
> for them, _even_ that my server can handle them, I still have to think
> of the user downloading a file etc....
Then, you do not want to check for the positions of the invalid characters
(as you said on your original post).
You want to run a preg_match (to detect) or preg_replace (to fix) on the
filename string.
--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-
Un ordenador no es un televisor ni un microondas, es una herramienta
compleja.
Re: strpos - and multiple needles?
am 28.12.2007 17:32:46 von My Pet Programmer
jodleren said:
> On Dec 28, 5:41 pm, Iv=E1n S=E1nchez Ortega
> escomposlinux.-.punto.-.org> wrote:
>> jodleren wrote:
>>> On Dec 28, 5:20 pm, Iv=E1n S=E1nchez Ortega
>>> escomposlinux.-.punto.-.org> wrote:
>>>> jodleren wrote:
>>>>> To check for invalid characters in a filename
>>>> Escape the filename, then.
>>> Sorry, I dont get this one.
>> OK... why do you need to check for invalid characters in a filename?
>>
>> In other words, where could a filename with "strange" characters cause=
>> damage to your system?
>=20
> Windows based systems do not allow certain characters. I need to check
> for them, _even_ that my server can handle them, I still have to think
> of the user downloading a file etc....
Been there, definitely.
preg_match('/^([a-zA-Z]\:)(\\[^\\/:*?<>"|]*(?
]))*(\.[a-zA-Z]{2,6})$/', $filename);
That one should only match valid filenames.
~A!
--=20
Anthony Levensalor
anthony@mypetprogrammer.com
Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Re: strpos - and multiple needles?
am 29.12.2007 08:12:57 von Tim Roberts
My Pet Programmer wrote:
>
>preg_match('/^([a-zA-Z]\:)(\\[^\\/:*?<>"|]*(?
>]))*(\.[a-zA-Z]{2,6})$/', $filename);
>
>That one should only match valid filenames.
There are two things here I don't understand. First, I don't understand
what you are trying to do with (?
there that caused the line to wrap.) As I read it, that says "fail if the
last character of the previous match was a space", but since the pattern
before it matches all the characters in the pattern after it, I don't see
how it could ever take effect.
Second, I don't get what you are trying to do with (\.[a-zA-Z]{2,6}). Is
that supposed to be matching an extension? That fails for "test.c".
Also, that requires a drive letter. Was that your intent?
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
Re: strpos - and multiple needles?
am 29.12.2007 08:29:40 von Anthony Levensalor
Tim Roberts said:
> My Pet Programmer wrote:
>> preg_match('/^([a-zA-Z]\:)(\\[^\\/:*?<>"|]*(?
>> ]))*(\.[a-zA-Z]{2,6})$/', $filename);
>>
>> That one should only match valid filenames.
>
> There are two things here I don't understand. First, I don't understand
> what you are trying to do with (?
> there that caused the line to wrap.) As I read it, that says "fail if the
> last character of the previous match was a space", but since the pattern
> before it matches all the characters in the pattern after it, I don't see
> how it could ever take effect.
>
> Second, I don't get what you are trying to do with (\.[a-zA-Z]{2,6}). Is
> that supposed to be matching an extension? That fails for "test.c".
>
> Also, that requires a drive letter. Was that your intent?
Actually, I made a mistake in posting that, grabbed a regExp from a
completely different RegEx scheme. I thought you were looking for a
windows workaround, and I completely spaced it and gave you one of their
..NET ones. Sorry about that.
This one is the one I meant to hand off.
preg_match('![^A-Z0-9_\.]!i',$filename)
If it matches, then the filename is no good, ok otherwise
Test cases:
lib.so - Ok
tbl_status - Ok
my file is cool.txt NOT Ok
\ this- isn't value NOT Ok
text.c - Ok
foo - Ok
superlong_with_underscores.png - Ok
ILikeTrucks!.jpg NOT Ok
REALLY sorry about the mix-up.
~A!
--
Anthony Levensalor
anthony@mypetprogrammer.com
Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Re: strpos - and multiple needles?
am 29.12.2007 15:51:00 von Jerry Stuckle
Anthony Levensalor wrote:
> Tim Roberts said:
>> My Pet Programmer wrote:
>>> preg_match('/^([a-zA-Z]\:)(\\[^\\/:*?<>"|]*(?
>>> ]))*(\.[a-zA-Z]{2,6})$/', $filename);
>>>
>>> That one should only match valid filenames.
>>
>> There are two things here I don't understand. First, I don't understand
>> what you are trying to do with (?
>> there that caused the line to wrap.) As I read it, that says "fail if
>> the
>> last character of the previous match was a space", but since the pattern
>> before it matches all the characters in the pattern after it, I don't see
>> how it could ever take effect.
>>
>> Second, I don't get what you are trying to do with (\.[a-zA-Z]{2,6}). Is
>> that supposed to be matching an extension? That fails for "test.c".
>>
>> Also, that requires a drive letter. Was that your intent?
> Actually, I made a mistake in posting that, grabbed a regExp from a
> completely different RegEx scheme. I thought you were looking for a
> windows workaround, and I completely spaced it and gave you one of their
> .NET ones. Sorry about that.
>
> This one is the one I meant to hand off.
> preg_match('![^A-Z0-9_\.]!i',$filename)
>
> If it matches, then the filename is no good, ok otherwise
> Test cases:
>
> lib.so - Ok
> tbl_status - Ok
> my file is cool.txt NOT Ok
> \ this- isn't value NOT Ok
> text.c - Ok
> foo - Ok
> superlong_with_underscores.png - Ok
> ILikeTrucks!.jpg NOT Ok
>
> REALLY sorry about the mix-up.
>
> ~A!
>
A space is also valid in Windows.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: strpos - and multiple needles?
am 31.12.2007 21:07:23 von Tim Roberts
Anthony Levensalor wrote:
>
>If it matches, then the filename is no good, ok otherwise
>Test cases:
>
>lib.so - Ok
>tbl_status - Ok
>my file is cool.txt NOT Ok
>\ this- isn't value NOT Ok
>text.c - Ok
>foo - Ok
>superlong_with_underscores.png - Ok
>ILikeTrucks!.jpg NOT Ok
I guess this depends on your definition of "no good". Both "my file is
cool.txt" and "ILikeTrucks!.jpg" are valid file names on both Windows and
Unix.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.