[PHP] count string within a string

2006-11-17 Thread Børge Holen
I've figured out I need the substr_count function.
However, counting strings like this: [1] [2] [3] ... [215] ... 
inside a large string element. damn. 
The string got quite a few different numbers and keys but the main key is 
alway within [] and is numeric.
I imagine I could use a $counter++.
I just dont see how I should aproach this.

Anyone got an Idea?

-- 
---
Børge
Kennel Arivene 
http://www.arivene.net
---

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] count string within a string

2006-11-17 Thread Eric Butera

On 11/17/06, Børge Holen [EMAIL PROTECTED] wrote:

I've figured out I need the substr_count function.
However, counting strings like this: [1] [2] [3] ... [215] ...
inside a large string element. damn.
The string got quite a few different numbers and keys but the main key is
alway within [] and is numeric.
I imagine I could use a $counter++.
I just dont see how I should aproach this.

Anyone got an Idea?

--
---
Børge
Kennel Arivene
http://www.arivene.net
---

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Apologies if I missed what you were after.. but maybe you might try this:

?php
$string = 'However, counting strings like this: [1] [2] [3] ... [215] ...';

$result = preg_match_all('/\[([0-9]+)\]/U', $string, $matches);
if (! $result )
   echo No matches\n;

echo Count: . count($matches[1]);
--

Results in:
Count: 4

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php