Jump to content

[SOLVED] Count Amount of time a Phraze appears in a string?


umarsa

Recommended Posts

Hello again..

 

I wanted to know if there is anyway that you can acount how many times a phraze is found in a string, for example:

 

$string = "Hello how are you today are you feeling well?";

 

Now i want to know how many times the word "are" appears in the string, is there a way i can make a php script to do this?

 

'are' appears "2" times, so is there a way?

excuse me, it is actually preg_match_all() that you want

http://us.php.net/manual/en/function.preg-match-all.php

preg_match will only return 0 for not found or 1 for found... preg_match_all will count the occurrences.

Mark also supplied a valid solution.

If you want to use preg_match_all() with your example, this is how.

<?php
$string = "Hello how are you today are you feeling well?";
echo preg_match_all("/are/", $string, $matches);
?>

Okay..

 

I went back to try the script and when i try to search my phraze

"[img]"

i get 27 results...

 

 

There is only one of them in my script..

 

So went and explored a bit and made this:

"/[[img]]/"

 

As the search thing but now that counts

"[img=http://" AND "]"

how do i fix this?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.