By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,118 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,118 IT Pros & Developers. It's quick & easy.

strpos - and multiple needles?

P: n/a
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?
Dec 28 '07 #1
Share this Question
Share on Google+
18 Replies


P: n/a
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

Dec 28 '07 #2

P: n/a
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
an*****@mypetprogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Dec 28 '07 #3

P: n/a
Iván Sánchez Ortega said:
jodleren wrote:
[snip]
preg_match("[abc]",$subject);
Oh, see, that's much better than what I did.

You might find reading something on regular expressions useful.
And so, it seems, would I. Thanks!

~A!
--
Anthony Levensalor
an*****@mypetprogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Dec 28 '07 #4

P: n/a
Iván Sánchez Ortega said:
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,
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?

--
Anthony Levensalor
an*****@mypetprogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Dec 28 '07 #5

P: n/a
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_*************************@hotmail.com
Jabber:iv*********@jabber.org ; iv*********@kdetalk.net
Dec 28 '07 #6

P: n/a
Iván Sánchez Ortega said:
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?
Oh, for me it was just idle curiosity. I'm not sure what the OP is after.

~A!

--
Anthony Levensalor
an*****@mypetprogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Dec 28 '07 #7

P: n/a
On Dec 28, 4:43*pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.orgwrote:
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
Dec 28 '07 #8

P: n/a
jodleren said:
On Dec 28, 4:43 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.orgwrote:
>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
preg_match("*[abc]*", $haystack); worked for me.

~A!

--
Anthony Levensalor
an*****@mypetprogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Dec 28 '07 #9

P: n/a
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_*************************@hotmail.com
Jabber:iv*********@jabber.org ; iv*********@kdetalk.net
Dec 28 '07 #10

P: n/a
On Dec 28, 5:20*pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.orgwrote:
jodleren wrote:
To check for invalid characters in a filename
Escape the filename, then.
Sorry, I dont get this one.
Dec 28 '07 #11

P: n/a
jodleren wrote:
On Dec 28, 5:20*pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.orgwrote:
>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.
Dec 28 '07 #12

P: n/a
On Dec 28, 5:41*pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.orgwrote:
jodleren wrote:
On Dec 28, 5:20*pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.orgwrote:
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....
Dec 28 '07 #13

P: n/a
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.
Dec 28 '07 #14

P: n/a
jodleren said:
On Dec 28, 5:41 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.orgwrote:
>jodleren wrote:
>>On Dec 28, 5:20 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.orgwrote:
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....
Been there, definitely.
preg_match('/^([a-zA-Z]\:)(\\[^\\/:*?<>"|]*(?<![
]))*(\.[a-zA-Z]{2,6})$/', $filename);

That one should only match valid filenames.

~A!


--
Anthony Levensalor
an*****@mypetprogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Dec 28 '07 #15

P: n/a
My Pet Programmer <an*****@mypetprogrammer.comwrote:
>
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 (?<![ ]). (I assume there was a space in
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, ti**@probo.com
Providenza & Boekelheide, Inc.
Dec 29 '07 #16

P: n/a
Tim Roberts said:
My Pet Programmer <an*****@mypetprogrammer.comwrote:
>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 (?<![ ]). (I assume there was a space in
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
an*****@mypetprogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Dec 29 '07 #17

P: n/a
Anthony Levensalor wrote:
Tim Roberts said:
>My Pet Programmer <an*****@mypetprogrammer.comwrote:
>>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 (?<![ ]). (I assume there was a space in
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.
js*******@attglobal.net
==================

Dec 29 '07 #18

P: n/a
Anthony Levensalor <an*****@mypetprogrammer.comwrote:
>
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, ti**@probo.com
Providenza & Boekelheide, Inc.
Dec 31 '07 #19

This discussion thread is closed

Replies have been disabled for this discussion.