21 :: How To Remove the New Line Character from the End of a Text Line in PHP?
If you are using fgets() to read a line from a text file, you may want to use the chop() function to remove the new line character from the end of the line as shown in this PHP script:<?php
$handle = fopen("/tmp/inputfile.txt", "r");
while ($line=fgets()) {
$line = chop($line);
# process $line here...
}
fclose($handle);
?>
|
22 :: How To Remove Leading and Trailing Spaces from User Input Values in PHP?
If you are taking input values from users with a Web form, users may enter extra spaces at the beginning and/or the end of the input values. You should always use the trim() function to remove those extra spaces as shown in this PHP script:<?php
$name = $_REQUEST("name");
$name = trim($name);
# $name is ready to be used...
?>
|
23 :: How to Find a Substring from a Given String in PHP?
To find a substring in a given string, you can use the strpos() function. If you call strpos($haystack, $needle), it will try to find the position of the first occurrence of the $needle string in the $haystack string. If found, it will return a non-negative integer represents the position of $needle. Othewise, it will return a Boolean false. Here is a PHP script example of strpos():<?php
$haystack1 = "2349534134345globalguideline16504381640386488129";
$haystack2 = "globalguideline234953413434516504381640386488129";
$haystack3 = "guideline234953413434516504381640386488129ggl";
$pos1 = strpos($haystack1, "globalguideline");
$pos2 = strpos($haystack2, "globalguideline");
$pos3 = strpos($haystack3, "globalguideline");
print("pos1 = ($pos1); type is " . gettype($pos1) . " ");
print("pos2 = ($pos2); type is " . gettype($pos2) . " ");
print("pos3 = ($pos3); type is " . gettype($pos3) . " ");
?>
This script will print:
pos1 = (13); type is integer
pos2 = (0); type is integer
pos3 = (); type is boolean
"pos3" shows strpos() can return a Boolean value
|
24 :: What Is the Best Way to Test the strpos() Return Value in PHP?
Because strpos() could two types of values, Integer and Boolean, you need to be careful about testing the return value. The best way is to use the "Identical(===)" operator. Do not use the "Equal(==)" operator, because it does not differentiate "0" and "false". Check out this PHP script on how to use strpos():<?php
$haystack = "needle234953413434516504381640386488129";
$pos = strpos($haystack, "needle");
if ($pos==false) {
print("Not found based (==) test ");
} else {
print("Found based (==) test ");
}
if ($pos===false) {
print("Not found based (===) test ");
} else {
print("Found based (===) test ");
}
?>
This script will print:
Not found based (==) test
Found based (===) test
Of course, (===) test is correct.
|
25 :: How To Take a Substring from a Given String in PHP?
If you know the position of a substring in a given string, you can take the substring out by the substr() function. Here is a PHP script on how to use substr():<?php
$string = "beginning";
print("Position counted from left: ".substr($string,0,5)." ");
print("Position counted form right: ".substr($string,-7,3)." ");
?>
This script will print:
Position counted from left: begin
Position counted form right: gin
substr() can take negative starting position counted from the end of the string.
|




Webmaster Said:
Thank you.
Krishna Said:
jayshri kinge Said:
koushik chatterjee Said:
sayar Said:
i got a lot of information thank you very much....
Remya Said:
This website is really helping for my studies, but it will be good when i get some more php Questions and answers,can you please send all the questions with answers, please..Expecting your reply
Thank You Remya
Niyas Ahamed Said:
yamini Said:
suman.p Said:
Sunil Kumar Said:
Thanks Sir
hardik Said:
Selim Said:
will it be possible to send me the question and answer ?
Subhu Said:
Mohd Faizan Said:
I am new on PHP/Mysql/Drupal so please send me the more interview question to me
Michal Blood Said:
JITENDRA KUMAR Said:
I am new to PHP/MYSQL/Joomla.I am really obtaining valuable information from you.You are really doing very good job. Thank you. Regards JITENDRA KUMAR
Varadha Said:
This Website helps me to know about PHP in a deeper way.
free Tutorials Said:
Palanisamy Said:
57 :: Are objects passed by value or by reference?
Not everything is passed by value. Since PHP 5, Objects are always based by reference
sajan watts Said:
Rangaballav swain Said:
Thank You
Rangaballav swain
sharmila Said:
diamond Said:
car rental chennai Said:
Heep up the good work just I must say thank you for this great list!.
Hotels chennai Said:
apartments for sale Said:
Sign Board Said:
kaleem Said:
kamal Said:
kamal Said:
AMEETA MUKHI Said:
tauseef ahmad Said:
Ali Said: