Hi; some time ago I did ask for help on a word count script to make it work in wp to add picture after a certain number of words when there is post, I got lots help here, but in the end I could not make it work because of double posts, anyway, I may work on that when I learn more php, but now I want to do a similiar thing but not in blog, just in a html page, it is not automated, because it will be only once, so I can have the script in the page or call it. I have gathered a script for counting word in the page, and working around it to add picture after certain number of words. the word count script works fine, the modified script does not have any error but it does not do what i suppose to do, well, I can not figure it out what I do wrongly. I appreciate if can you help me out here. here is the word count script that work: <?php $thispage = "http://localhost".$_SERVER['PHP_SELF']."?nocount=nope"; if(isset($_GET["nocount"])){ $nocount = $_GET["nocount"]; }else{ $nocount=""; }; if($nocount!="nope"){ $contents = file_get_contents($thispage); str_replace("\n", " ", $contents); if(preg_match("/<!--BEGIN WORD COUNT-->(.+?)<!--END WORD COUNT-->/sU",$contents,$matches)){ echo "Word count: ".count(preg_split("/\s+/",strip_tags($matches[1]),-1,PREG_SPLIT_NO_EMPTY))."<br>\n"; }; }; //$ar = preg_split("/\s+/",strip_tags($matches[1]),-1,PREG_SPLIT_NO_EMPTY); ?> <!--BEGIN WORD COUNT--> this is the main article that is being tested, this is the main article that is being tested, this is the main article that is being tested,this is the main article that is being tested,this is the main article that is being tested,this is the main article that is being tested,this is the main article that is being tested. his is the main article that is being tested,this is the main article that is being tested, <!--END WORD COUNT--> Code (markup): and here is the above script that has been modified: <?php $filepath = 'images/'; $file="pic_1.gif"; $count = $tally = $totalcount = 0; $printme = ""; $thispage = "http://localhost".$_SERVER['PHP_SELF']."?nocount=nope"; if(isset($_GET["nocount"])){ }else{ $nocount=""; }; if($nocount!="nope"){ $contents = file_get_contents($thispage); str_replace("\n", " ", $contents); if(preg_match("/<!--BEGIN WORD COUNT-->(.+?)<!--END WORD COUNT-->/sU",$contents,$matches)){ echo "Word count: ".count(preg_split("/\s+/",strip_tags($matches[1]),-1,PREG_SPLIT_NO_EMPTY))."<br>\n"; }; }; //$ar = preg_split("/\s+/",strip_tags($matches[1]),-1,PREG_SPLIT_NO_EMPTY); $x=0; $x < $contents; $x++; $tally++; $printme .= $contents[$x] . " "; if($tally == 50 ) $printme .= "\n" . '<img scr="'. $filepath.$file.'"/>'; $tally = 0; $totalcount += $contents; ?> <!--BEGIN WORD COUNT--> this is the main article that is being tested, this is the main article that is being tested, this is the main article that is being tested,this is the main article that is being tested,this is the main article that is being tested,this is the main article that is being tested,this is the main article that is being tested. his is the main article that is being tested,this is the main article that is being tested, <!--END WORD COUNT--> appreciate your help Code (markup):
Without trying to write this function for you, I can see that you have major problems. You are trying to run a for loop without properly declaring it. The for loop starts here: for ( $x=0; $x < $contents; $x++ ) { # do stuff } Secondly, $contents is the list of words. You want the number of words when you are testing the value of $x in the for loop.
thank you for reply; I made the changes for the for loop, but I don't know how to make the word count work instead of word list, do I have to change the part of counting script that is working now? the script shows the number of word, how can I use the $content though? appreciate more details. thanks
You got the word count when you used the statement: "Word count: ".count(preg_split . . . . Simply capture the result of "count(preg_split . . . ." into a variable, such as $wordCount To display it as above use: echo "Word Count: " . $wordCount . "<br>\n"; Change the start of the for loop to: for ( $x=0; $x < $wordCount; $x++ ) { # do stuff } You will access the individual words in the array $contents via $contents[$x] You really should review the thread http://forums.digitalpoint.com/showthread.php?t=94383 because the code snippet ended up being created in that thread. You can see again how we made it work.
thanks for reply; yes I was testing the same thing, but I can not find out the what is wrong, I get this error: Warning: file_get_contents(http://localhost/allinfo/__abc/foo22.php?nocount=nope) [function.file-get-contents]: failed to open stream: HTTP request failed! in c:\Inetpub\wwwroot\allinfo\__abc\foo22.php on line 19 Fatal error: Maximum execution time of 60 seconds exceeded in c:\Inetpub\wwwroot\allinfo\__abc\foo22.php on line 19 and on line 19: $contents = file_get_contents($thispage); Code (markup): here is how it looks( i did not paste the text part at the end here) <?php $filepath = 'images/'; $file="pic_1.gif"; $count = $tally = $totalcount = 0; $printme = ""; $thispage = "http://localhost".$_SERVER['PHP_SELF']."?nocount=nope"; if(isset($_GET["nocount"])){ }else{ $nocount=""; }; if($nocount!="nope"){ $contents = file_get_contents($thispage); str_replace("\n", " ", $contents); if(preg_match("/<!--BEGIN WORD COUNT-->(.+?)<!--END WORD COUNT-->/sU",$contents,$matches)){ echo "Word count: ".count(preg_split("/\s+/",strip_tags($matches[1]),-1,PREG_SPLIT_NO_EMPTY))."<br>\n"; }; }; $wordCount = preg_split("/\s+/",strip_tags($matches[1]),-1,PREG_SPLIT_NO_EMPTY); for ( $x=0; $x < $wordCount; $x++ ) { $printme .= $contents[$wordCount]." "; if($wordCount==10){ $printme .= "\n" . '<img scr="'. $filepath.$file.'"/>'; } } ?> PHP: thanks
You need to correct the error generated by file_get_contents. Sometimes these calls require that the variable be contained with quotes. In order to open a URL your php.ini file needs to be set to allow_url_fopen = 1 -- or-- allow_url_fopen = On Then you need to know file exists. And, if you are accessing it on the "localhost" in this manner, you need to have a web server running and the file must be in a directory which the server can access. Since you are calling a php file as a webpage -- it is trying to execute. Therefore, you must be certain that it is working correctly and returning with text! If all you want to do is open a local file -- open it without the http protocol