Jump to content

PHP Search


MasterHernan

Recommended Posts

The answer to your question is "Yes", and...if the example you provided is the practical use you had in mind...the extension to that answer is "there are easier ways"...Most good text editing software, comes with features that allow you to do word counts for specific words, so I'd just recommend feeding your essay in to a program like that if that's what you need the function for.

 

Otherwise, if you've got a broader idea in mind, the answer to your question lies in the use of Regular Expressions for text matching...using the preg_match() in PHP, you can match a specific string pattern, and return every match to that pattern in to an array...If Regular Expressions are a new idea to you, I highly recommend that you take a look at http://www.regular-expressions.info/.  They've got some great tutorials and reference on using regular expressions that should really help you.  Once you understand them, you can just look at the PHP manual for information about preg_match.

 

 

Link to comment
Share on other sites

Barand posted a nice and simple approach, but as we're in strings manipulation:

 

For counting all the characters in a text:

$text = 'This is just some short text';
echo count(explode(' ', $text)); //it will print => 6

 

For printing how many times each character is used in a text:

$text = 'This is just some other short text';
foreach(count_chars($text, 1) as $key=>$val){
      echo "Character: " . chr($key) . " is used $val times<br />";
}

Link to comment
Share on other sites

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.