Jump to content

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


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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.