I'm new to php and the book I'm forced to use is for intermediate-advanced users; needless to say, confusing as hell. I'm suppose to put 3 paragraphs in a variable, which I was going to call $para using html paragraph tags. Then use the preg_match_all expression to pull the number of times a word such as "nature" is found within the 3 paragraphs. I'll also assume that I could use the same method to output the number of paragraphs by the <p> tag. I somewhat understand how to use preg_match_all to find a word and execute the script to output it into an array. Any help is appreciated.

    You might want to start with some code yourself and then we can provide feedback about where you are doing it right or not.

      You might want to look at [man]substr_count/man for cases where you are looking for a specific set of characters where a regular expression is not needed.

        No problem on posting what I have. This is an online class I'm taking. So assistance is almost nil. So, I'm having to teach myself. Here is a short blurb on what I'm trying to do.

        1. I needed to put 3 paragraphs into a variable, making sure that you put the paragraphs in HTML paragraph tags. (3 paras are in the script)
        2. Using preg_match_all and array length, find the number of times that the word "nation" appears in the speech above. Output the result.

        This is what I can figure out from looking at my book:

        <?php

        $para = '<p>Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.</p>< br/>
        
        	<p>Now we are engaged in a great civil war, testing whether that nation, or any nation, so conceived and so dedicated, can long endure. We 	are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.</p>< br/>
        
        	<p>But, in a larger sense, we can not dedicate - we can not consecrate - we can not hallow - this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us - that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion - that we here highly resolve that these dead shall not have died in vain - that this nation, under God, shall have a new birth of freedom - and that government of the people, by the people, for the people, shall not perish from the earth.</p>';
        
        preg_match_all ('/nation/', $para, $matches);
        $para = array('Nation' => 'one' , 'null' => 'zero');
        var_dump ($para);

        ?>

        Thanks for any help.

          At its simplest:

          preg_match_all('/nation/i', $para, $matches);
          $count = count($matches[0]);
          

          However, things could get a lot messier if you have to account for any possibility that "nation" might appear within a HTML tag (such as "<p class='nation'>") and as such should not be included in the count.

            Thanks NogDog but I have a question. How is this using a array length or is that the purpose of the $count line and do I need to use a var_dump or echo to display the output of this? Thanks

              See http://www.php.net/count for what count() does. (I don't mean to be difficult, but to get you used to reading the manual, which ultimately "knows" more than I do.) I think that will answer your question about array length.

              As to what you want to do to display the results, that is entirely dependent on what you want to do. If you want to make it HTML output, then I'd probably just use echo, e.g.:

              echo "<p>The word 'nation' appeared $count times in the text.</p>";
              

              If you just want a "quick and dirty" output for debug purposes with some info that a "normal" user would not care about, then var_dump() would be a good choice:

              var_dump($count);
              

                Thanks NogDog. Yea I've been doing a good bit of searching and reading. I saw descriptions on the use of array lengths which seemed to be different than what was written here. That's why I asked. Also, I went with the var_dump as the output because that's what's used in the chapter I'm on.

                  Just keep in mind that print_r and var_dump are intended for use as debugging/exploration utilities - not for use in real life 🙂

                    Write a Reply...