Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Specific Characters Again 1

Status
Not open for further replies.

likelylad

IS-IT--Management
Joined
Jul 4, 2002
Messages
388
Location
GB
I thought it might be better for me to start this one again

I have the following code

$string="ÀyÀuÿpÿpÿaÀsdfer";
preg_match_all("[À-ÖØ-öø-ÿ]",$string,$reg,PREG_PATTERN_ORDER);
print count($reg);

The produced answer is 1
I would like the answer to be 6 which are the characters in positions 1,3,5,7,9 and 11
$string will change as it will be taken from a file.

How do I procede, is there any other function that I can use.
 
I see a couple of things:

1. Your regular expression pattern is not enclosed in delimiters, for example forward slashes:
Code:
# wrong:
$pattern = "[À-ÖØ-öø-]";
# right:
$pattern = "/[À-ÖØ-öø-]/"

2. The result "6" that you expect is wrong. It is really only three - I see only a match for position 0,2,7

3. Of course the count for $reg is 1. The PHP documentation says:
PREG_PATTERN_ORDER

Orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on.


In your case there are no subpatterns, so the count is 1. The only element however itself is an array since the return is a two dimensional array.

Hope this helps.
 
point1
I have changed that

point 2
something happened when I posted, so now lets suppose the answer should be 3

point 3
how do I get it to produce 3??????
 
Examine the first element of the return array as an array:
Code:
echo ("There were ".count($reg[0])." matches");
You are dealing here with an array element that is an array itself, hence, two-dimensional.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top