Solved PHP Question

Solved issue

chlyde

New Member
Joined
Dec 21, 2021
Messages
7
Reaction score
1
Credits
99
hello,
You guys did such a great job getting me to run thru what must've happened last time, I thought I'd do it again 8 )__.
I'm using PHP in Included file to run thru the file system and print the .txt files, therein, to screen and I am baffled. It seems to want to print every file & below 100 words ( a no-no). Below is the script (variables made clearer). I just thought to use $wCount (doesn't work either) in Switch but I'd still like to know why this won't work {8 ]__. thank you for your input
[this is funny :
i put echo "hello > 100 !"; in the case true, and, it only printed it in those greater than 100 ; >]
phfff ! I had some html tags in one of the .txt files (removed tags and fixed the problem)!! I thought i'd leave this up a few days to let others know what could happen (so, simple)
<?php echo mains();
function mains(){
global $scan=(the scanned folder/for text files);
global $br="<br/>";
global $hr="<hr size='10'>";
foreach($scan as $a){
$reading=fread(fopen("$a","r"),filesize("$a"));
$wCount=str_word_count($reading);
if(is_file($a)){
switch(str_word_count($reading)>100){
case true:
echo $wCount.$br;
echo $reading.$br.$hr;break;}}}} ?>
 
Last edited:


TheProf

Well-Known Member
Joined
Jun 15, 2021
Messages
314
Reaction score
323
Credits
2,915
Maybe something like this might work:

<?php $folderPath = '/home/ubuntu/test'; $br = PHP_EOL; $hr = "<hr size='10'>"; $files = glob($folderPath . '/*.txt'); foreach ($files as $file) { $reading = file_get_contents($file); $wordCount = str_word_count($reading); if ($wordCount > 100) { echo "File: $file$br"; echo "Word Count: $wordCount$br$br"; } } ?>
This seems to work when I test this script out in my lab. These are the steps I took:
  1. Define the folder path where the text files are going to reside
  2. Lines breaks when outputting the results
  3. The array $files defines a glob() function to fetch an array of file paths that much the pattern (.txt files)
  4. A foreach loop to iterate over each file in the $file array
 

KGIII

Super Moderator
Staff member
Gold Supporter
Joined
Jul 23, 2020
Messages
10,673
Reaction score
9,168
Credits
88,613
Hello. Welcome to the forum.

Why is this under General Server?

For some reason, they edited their question. They did so multiple times.

I'm just going to close the thread. There's not much hope of salvaging it.
 

wizardfromoz

Administrator
Staff member
Gold Supporter
Joined
Apr 30, 2017
Messages
9,186
Reaction score
8,138
Credits
39,448
I have reverted this to the way it should be and am moving it to Command Line.

@chlyde please don't delete or edit to mean nothing your first Posts in future.

TIA

Wizard
 

KGIII

Super Moderator
Staff member
Gold Supporter
Joined
Jul 23, 2020
Messages
10,673
Reaction score
9,168
Credits
88,613
I have reverted this to the way it should be and am moving it to Command Line.

LOL I thought about doing that but couldn't decide which edit was best and figured, while horribly annoying, a user can edit their post so long as it doesn't violate the rules.

I suppose we can use this as precedent as this isn't the first time this has happened.
 

Members online


Latest posts

Top