MasterHernan Posted August 3, 2007 Share Posted August 3, 2007 I wanted to know if there is a way with PHP to read files and just pick out certain words. For example, I have an essay paper and I want to see if with php I can find out how many times I said the word "for". Quote Link to comment https://forums.phpfreaks.com/topic/63198-php-search/ Share on other sites More sharing options...
stlewis Posted August 3, 2007 Share Posted August 3, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/63198-php-search/#findComment-314987 Share on other sites More sharing options...
MasterHernan Posted August 3, 2007 Author Share Posted August 3, 2007 Thanks a lot, ill look into it. The essay thing was just an example from the actual thing I'm trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/63198-php-search/#findComment-314994 Share on other sites More sharing options...
Barand Posted August 3, 2007 Share Posted August 3, 2007 try <?php $essay = "An example for you showing how to search for 'for' and count the occurences"; $count = substr_count($essay, 'for'); echo $count; // --> 3 ?> Quote Link to comment https://forums.phpfreaks.com/topic/63198-php-search/#findComment-315134 Share on other sites More sharing options...
Fadion Posted August 4, 2007 Share Posted August 4, 2007 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 />"; } Quote Link to comment https://forums.phpfreaks.com/topic/63198-php-search/#findComment-315315 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.