PHP SPLIT

Posted by chaabant on Jan 4, 2009

hi..can anyone please solve my problem?

i have text input and a submit button.

The user will insert sentence/question/any text in the text input.Then, when he/she press the submit
button, the input will be keep in the database(mysql). After that, i want to split the input(let say a sentence) into words(array).

Here is an example:

Input: Differentiate between array and arraylist.

After splitting:

=>Differentiate
=>between
=>array
=>and
=>arraylist
=>.

I wonder how mysql handle the input after splitting it?is it in a field or each index of array(each word) in the difference field?because after splitting it, i want to match the input(which is now in form of words not a sentence) with a library i made in mysql.

Let say, in my library there are 3 fields:weight1,weight2,weight3.The word ‘what’ was in field weight1 in my
library.

So, the question is: how can i split the input into words and then match it with my library so that i can state the input is in which weight(weight1/weight2/weight3).

Please help me.
Thank you.


What does PHP means

Posted by chaabant on Dec 22, 2008

As you know , on this site you can ask PHP Questions , and if u know the answers i will be more than happy to answer them .

To ask a question simply click on the HEADER TAB (PHP QUESTIONS) And submit the form .

Here is the recent question i received :

vikas wrote:

What is The full form of PHP?
1) Personal Home Page
2) PHP Hypertext Preprocessor

Plz give me description of the option?

Regards,
Vikas

I think you mean what does PHP Stand for …

it does stand for Hypertext Preprocessor it’s A script language and interpreter .

So the Answer would be (2) , a Personal Home Page would be the main page of a website where you could have personal stuff or maybe a blog where you write your life experiences …

ex: this is my personal blog : CHAABAN , the first page is called the home page .

Hope this answered your question ;)


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 …