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

Fastest way to look for an array of char?

P: n/a
Hi,

If I have a string, (variable len), and I am looking for the first position
of one char in array starting from position 'x'

For example,

// the 'haystack'
$string = "PHP is great, php is ok";
// the needles
$char[] = 'P';
$char[] = 'r';
$char[] = 'k';

I would search the $string for the first $char starting from 3 and it would
return 7, (for 'g').

I could use many strpos(...) but that does not sound very efficient.
So what would be the fastest way to get the first position?

And, I am using PHP 4.x, not 5, (otherwise I guess I would use strpos(...)).

Many thanks in advance.

Simon
Jul 17 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Simon wrote:
Hi,

If I have a string, (variable len), and I am looking for the first
position of one char in array starting from position 'x'

For example,

// the 'haystack'
$string = "PHP is great, php is ok";
// the needles
$char[] = 'P';
$char[] = 'r';
$char[] = 'k';

I would search the $string for the first $char starting from 3 and it
would return 7, (for 'g').
Based on what?
I could use many strpos(...) but that does not sound very efficient.
So what would be the fastest way to get the first position?


If you mean that you want for all characters in char find the minimal
position in $string?

Hash each unique character, with it's position, e.g:

P => 0
H => 1
=> 3
i => 4
s => 5
g => 7
r => 8
e => 9
a => 10
t => 11
, => 12
p => 13
h => 14
o => 20
k => 21

(if I made no mistakes). Then you can do something like:

assign the position of the first item in char to $min
and keep the char in $firstchar;
for the next item in char up until and including the last one:
if the position of this item < $min:
$min = postion
$firstchar = item

It depends also on exactly what you want to do, it's quite possible that
doing just the n searches over the string is faster than the above hash
table stuff.
--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Jul 17 '05 #2

P: n/a
> Hi,

If I have a string, (variable len), and I am looking for the first
position of one char in array starting from position 'x'

For example,

// the 'haystack'
$string = "PHP is great, php is ok";
// the needles
$char[] = 'P';
$char[] = 'r';
$char[] = 'k';

I would search the $string for the first $char starting from 3 and it
would return 7, (for 'g').

^^^^^^^^^^^^^^^ sorry that should be "return 8 (for 'r')", because i am
looking for 'P', 'r' and 'k', not 'g'.

Simon
Jul 17 '05 #3

P: n/a
>
Hi,

If I have a string, (variable len), and I am looking for the first
position of one char in array starting from position 'x'

For example,

// the 'haystack'
$string = "PHP is great, php is ok";
// the needles
$char[] = 'P';
$char[] = 'r';
$char[] = 'k';

I would search the $string for the first $char starting from 3 and it
would return 7, (for 'g').
Based on what?


Sorry I made a typo, it should have been ...
"I would search the $string for the first $char starting from 3 and it would
return 8, (for 'r')."

'r' been one of the char I looking for.
I could use many strpos(...) but that does not sound very efficient.
So what would be the fastest way to get the first position?


If you mean that you want for all characters in char find the minimal
position in $string?


Sorry the typo made the whole thing unclear.
I want the fastest way to find the position of one of many characters in a
string.

Like look for the first of 'P' , 'r', 'k' in $string.

Simon
Jul 17 '05 #4

P: n/a
Simon wrote:
Hi,

If I have a string, (variable len), and I am looking for the first
position of one char in array starting from position 'x'

For example,

// the 'haystack'
$string = "PHP is great, php is ok";
// the needles
$char[] = 'P';
$char[] = 'r';
$char[] = 'k';

I would search the $string for the first $char starting from 3 and
it would return 7, (for 'g').


Based on what?


Sorry I made a typo, it should have been ...
"I would search the $string for the first $char starting from 3 and it
would return 8, (for 'r')."

'r' been one of the char I looking for.
I could use many strpos(...) but that does not sound very efficient.
So what would be the fastest way to get the first position?


If you mean that you want for all characters in char find the minimal
position in $string?


Sorry the typo made the whole thing unclear.
I want the fastest way to find the position of one of many characters
in a string.

Like look for the first of 'P' , 'r', 'k' in $string.


Fastest way most likely is, just look it up.

If $char is very big, and $string very long (and some characters appear
at the very end for the first time) the hash table trick might be
faster.
--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Jul 17 '05 #5

P: n/a
>
Fastest way most likely is, just look it up.

If $char is very big, and $string very long (and some characters appear
at the very end for the first time) the hash table trick might be
faster.


$char and $string are never really big, $char is up to 5 chars and $string
at most up to 1024.
I guess I am been overzealous.

I just think that looking for the possible position of 5 chars and returning
the lowest position seems like the long way around.

Simon
Jul 17 '05 #6

P: n/a
Simon wrote:

Fastest way most likely is, just look it up.

If $char is very big, and $string very long (and some characters
appear at the very end for the first time) the hash table trick might
be faster.
$char and $string are never really big, $char is up to 5 chars and
$string at most up to 1024.
I guess I am been overzealous.


So worst case means: 5 x 1024 steps. You can speed this up by going over
the array once, and store the position of each character in a hash, e.g.

for each character in the string:
if character not in hash, use character as key
and position as value.

This means you can now almost in one step (the hash can have some
overhead) look up the position for each item in $char, and hence find
the lowest one:
I just think that looking for the possible position of 5 chars and
returning the lowest position seems like the long way around.


See above. It depends a bit on the overhead of building the hash table,
which technically is just 1024 steps, but it's possible that doing the 5
lookups is faster.

In CS terms, building the hash is O( n ), doing each look up is O( 1 )
so in total the time is O( n ).

Doing 5 look ups in the entire string is 5 x O( n ), and hence total
time is O( n ) too.

Which one is the fastest depends on how PHP does things. Just a loop
like:

minpos = length $string; ( which is always an invalid position)
for i = 0 .. max:
find position of char[ i ]in $string;
if found, and position < minpos:
keep position
keep character

Might be more readable then the possible faster hash approach.

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Jul 17 '05 #7

P: n/a
"Simon" <sp********@myoddweb.com> wrote in message
news:38*************@individual.net...
Hi,

If I have a string, (variable len), and I am looking for the first position of one char in array starting from position 'x'


What exactly are you trying to do? I suspect regular expression is the
solution here.
Jul 17 '05 #8

P: n/a
>> Hi,

If I have a string, (variable len), and I am looking for the first

position
of one char in array starting from position 'x'


What exactly are you trying to do? I suspect regular expression is the
solution here.


If I wanted to find the pos of a char in a string I would use strpos(...).
But what I want to do is find one of many char in a string.

So ...
// the 'haystack'
$string = "PHP is great, php is ok";
// the needles, (the array of chars I am looking for)
$char[] = 'P';
$char[] = 'r';
$char[] = 'k'

And then I would look for the position of the first possible char in the
string...

In PHP5 you can use strpos(...) with arrays, but what about PHP4?

Speed been important I was wondering what the fastest way might be to look
for one of many char in a string.

I also suspect that RegEx might help, but I am not too good at it.
And what about if I want to start looking from position X rather than search
the whole string?

Simon
Jul 17 '05 #9

P: n/a
"Simon" <sp********@myoddweb.com> wrote in message
news:38*************@individual.net...
If I wanted to find the pos of a char in a string I would use strpos(...).
But what I want to do is find one of many char in a string.

So ...
// the 'haystack'
$string = "PHP is great, php is ok";
// the needles, (the array of chars I am looking for)
$char[] = 'P';
$char[] = 'r';
$char[] = 'k'

And then I would look for the position of the first possible char in the
string...
Surely you'll use the index some where down the line to extract a sub-string
from the text? An index to a string really doesn't have much other use other
than that.
I also suspect that RegEx might help, but I am not too good at it.
And what about if I want to start looking from position X rather than search the whole string?


You can obtain the index of the match by passing the PREG_OFFSET_CAPTURE
flag. To start from a particular position, just specify in the pattern that
the match should have any least x number of any character first:

/(?<=.{8})[Prk]/

Jul 17 '05 #10

P: n/a
>>
And then I would look for the position of the first possible char in the
string...
Surely you'll use the index some where down the line to extract a
sub-string
from the text? An index to a string really doesn't have much other use
other
than that.


Yes, but I use the string for editing the actual string.
So knowing the position of the char is more useful than the string.

But getting PREG_OFFSET_CAPTURE might give me the best of both worlds.
I also suspect that RegEx might help, but I am not too good at it.
And what about if I want to start looking from position X rather than

search
the whole string?


You can obtain the index of the match by passing the PREG_OFFSET_CAPTURE
flag. To start from a particular position, just specify in the pattern
that
the match should have any least x number of any character first:

/(?<=.{8})[Prk]/


sorry i am not sure i follow,

Are you saying that i should do.

preg_match( " /(?<=.{8})[Prk]/", $string, $match, PREG_OFFSET_CAPTURE );

Does "(?<=.{8}") mark the starting posision 8? and [Prk] mean any one char
what if one of the char is a special char like "[" or "{" and so on.

Simon
Jul 17 '05 #11

P: n/a

"Simon" <sp********@myoddweb.com> wrote in message
news:39*************@individual.net...

And then I would look for the position of the first possible char in the string...


Surely you'll use the index some where down the line to extract a
sub-string
from the text? An index to a string really doesn't have much other use
other
than that.


Yes, but I use the string for editing the actual string.
So knowing the position of the char is more useful than the string.

But getting PREG_OFFSET_CAPTURE might give me the best of both worlds.
I also suspect that RegEx might help, but I am not too good at it.
And what about if I want to start looking from position X rather than

search
the whole string?


You can obtain the index of the match by passing the PREG_OFFSET_CAPTURE
flag. To start from a particular position, just specify in the pattern
that
the match should have any least x number of any character first:

/(?<=.{8})[Prk]/


sorry i am not sure i follow,

Are you saying that i should do.

preg_match( " /(?<=.{8})[Prk]/", $string, $match, PREG_OFFSET_CAPTURE );

Does "(?<=.{8}") mark the starting posision 8? and [Prk] mean any one char
what if one of the char is a special char like "[" or "{" and so on.


(?<= ) means a lookbehind assertion, meaning a condition that needs to be
fulfilled previously. In this case we have a condition of .{8}. The dot
means any character. {8} means exactly 8 characters.
Jul 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.