Occurrence of a word in a sentence using php

Posted by chaabant on Jan 9, 2008

Can someone please post a code that will return a number of how many instances of a string are in a string? For example:

“my name is tim tim is a dumb name i hate people named tim”

I want a code to return “3″ (for the 3 instances of tim) for that. Please help!

here is the code :

  1. <?php
  2.  
  3. $i=0; // number of occurrence
  4. $msg = "my name is tim tim is a dumb name i hate people named tim"; // The Sentence
  5. $word = "tim"; // Word to Search
  6.  
  7.  
  8. $txt = explode(" ", $msg); // place the sentence in an array
  9.  
  10.  
  11.  
  12. foreach ($txt as $key=>$val) {
  13.  if($val == $word) {
  14.      $i++ ;
  15.    } // end if
  16.  } // end for
  17.  
  18.  
  19. echo "The Occurrence of $word in the Sentence is : <b> $i </b> ";
  20.  
  21. ?>

Result :

The Occurrence of tim in the Sentence is : 3

Take note that in php there is a difference when using a capital letter , Tim is Different than tim ; A Solution for this would be to use the string strtolower ( string $str ) function .

in our case :

  1. $msg = "my name is tim tim is a dumb name i hate people named tim"; // The Sentence
  2. $msg = strtolower($msg);

Hope this answer your question ;)


Generate Numbers in PHP

Posted by chaabant on Jan 5, 2008

I have no clue about programming and my knowledge only goes as far as excel! …was wondering if someone could lead me in the direction of what program to use to do this:

I basically need 50,000 numbers, each 11 digits long outputted into a text file (.txt)
For example 07850069737

The conditions are:

1st digit must be a zero
2nd digit must be a seven
3rd digit can be a 5 or 7 or 8 or 9
All the other digits are random
Each 11 digit number can only appear once in the 50,000.

So the text file will list the numbers like this (one number per line)

07543098434
07984304577
07948498357
07859488334

I think it’s pretty simple but not sure where to start.

Thanks in advance!

This is a kind of solution , but it’s not 100% it don’t test for uniqueness of the numbers .. you can add them to a database and set the field as unique .

  1. <?php
  2.  
  3.  
  4.  
  5. $myFile = "test.txt"; // The file that we will write to
  6. $fh = fopen($myFile, ‘w’) or die("can’t open file"); // Open the File
  7.  
  8.  
  9. $num1 = "07"; // first 2 numbers
  10.  
  11.  
  12. $var2 = array(5,7,8,9); // 3rd digit
  13.  
  14.  
  15. for($i=0 ; $i < 50000 ; $i++) {
  16.  
  17. srand((float) microtime() * 10000000);
  18.  
  19. // generating the 3rd digit randomnly
  20. $tnum2 = array_rand($var2);
  21. $num2 = $var2[$tnum2] ;
  22.  
  23.  
  24.  
  25. $num3 = rand(10000000,99999999); //generating last 8 digits
  26.  
  27.  
  28. // concatenating all together
  29. $real = $num1.$num2.$num3."\n" ;
  30.  
  31. fwrite($fh, $real);
  32.  
  33. }
  34.  
  35. fclose($fh); // close the file
  36.  
  37. echo "Done !";
  38.  
  39. ?>

Tested this on my local server and got some results :

07952105102
07980381164
07552621459
07786116027
07848509826
07731585388
07886201171
07790208435
07915254211
07758831481
07784220886
07933436584
07752843933
07824007568
07721466979
07914276428
07537883300
07584597167
07541311035
07588024902
07741497802
07943461608
07839547729
07862003784
07884459838
07772586364 …