Welcome Guest, Not a member yet? Register   Sign In
how to find and count the words in a string?
#1

[eluser]basty_dread[/eluser]
Hello, I need your help guys.


this is my code:

Code:
$words = "this is my page and the page is the only page that is nice";

my problem is i want to search the word "page" in the $words and count it if how many "page" word is in $words.


Please help and thank you.

in the example,
i found 3 words that is equal to the word "page"
#2

[eluser]Aken[/eluser]
Native PHP functions:

substr_count(): Count the number of times $needle shows up in $haystack. Con: if "page" shows up inside part of another word, it will consider it a match and count it.

preg_match(): Use regular expressions to perform a check if there's a match. Using the optional third parameter, it will output an array of matches. You can count that array for your total.
#3

[eluser]saidai jagan[/eluser]
$words = "this is my page and the page is the only page that is nice";
echo substr_count($words, ' page ');
#4

[eluser]basty_dread[/eluser]
wow thank you..
thanks for the reply guys..


How about this: i have a ckeditor.. and i want to count all the words in the ckeditor.. i used this code:

Code:
$vContent = "<h1 style="text-align: center;">
    <span style="font-family: comic sans ms,cursive;">reverse osmosis<br />
    </span></h1>";

$count = count(explode(" ", $vContent));

but my problem is it also counts the html tags. what should i do?
thank you for helping.
#5

[eluser]basty_dread[/eluser]
is there a way like this?
can ckeditor count everyword that is being edited?
#6

[eluser]saidai jagan[/eluser]
Code:
$search = '@<[\/\!]*?[^<>]*?&gt;@si'; // Strip the HTML tags
$replace=''; // replacing with none
$strings='<h1 style="text-align: center;">
    <span style="font-family: comic sans ms,cursive;">this is my page and the page is the only page that is nice<br />
    </span></h1>';
$non_html_text = preg_replace($search, $replace, $strings);
echo substr_count($non_html_text, ' page ');
#7

[eluser]basty_dread[/eluser]
I think you mis understood my explanation..

what i mean is that in ckeditor.

i want to count all the words in the ckeditor disregarding the html tags.
counting the words in the ckeditor's design view.

is that possible?
#8

[eluser]basty_dread[/eluser]
for example

$Content = "<h1 style="text-align: center;">
<span style="font-family: comic sans ms,cursive;">this is my page and the page is the only page that is nice<br />
</span></h1>";


// the only words that will appear in the design view of ckeditor is this:

this is my page and the page is the only page that is nice

// what i want is to count only the words in design view.

is it possible?


thank you very much.
i hope you could answer this saidai jagan..
thank you
#9

[eluser]saidai jagan[/eluser]
Here this is what we done.

Code:
$search = '@<[\/\!]*?[^<>]*?&gt;@si'; // Strip the HTML tags
$replace=''; // replacing with none
$strings='<h1 style="text-align: center;">
    <span style="font-family: comic sans ms,cursive;">this is my page and the page is the only page that is nice<br />
    </span></h1>';
echo $non_html_text = preg_replace($search, $replace, $strings); // here we are removing the html chars
// here it will retun this is my page and the page is the only page that is nice
echo '<br>'.substr_count($non_html_text, ' page '); // it will echo 3
#10

[eluser]Twisted1919[/eluser]
Code:
$html_content '<h1 style="text-align: center;">
    <span style="font-family: comic sans ms,cursive;">this is my page and the page is the only page that is nice<br />
    </span></h1>';

1. $content = strip_tags($html_content)
2. echo substr_count($content, ' page ')




Theme © iAndrew 2016 - Forum software by © MyBB