How can we extract string 'abc.com ' from a string http://info@abc.com using regular expression of php?How can we extract string "abc.com" from a string "mailto:info@abc.com ?subject= Feedback" using regular expression of PHP?What are the different functions in sorting an array?How To Read the Entire File into a Single String?How To Turn On the Session Support?How can we create a database using PHP and MySQL ?How can I execute a PHP script using command line? ~ Interview Questions & Answers

Sunday, February 3, 2008

How can we extract string 'abc.com ' from a string http://info@abc.com using regular expression of php?

We can use the preg_match() function with "/.*@(.*)$/" as the regular expression pattern.
For example:
preg_match("/.*@(.*)$/","http://info@abc.com",$data);
echo $data[1];

How can we extract string "abc.com" from a string "mailto:info@abc.com ?subject= Feedback" using regular expression of PHP?

$text = "mailto:info@abc.com?subject=Feedback";
preg_match('|.*@([^?]*)|', $text, $output);
echo $output[1];
Note that the second index of $output, $output[1], gives the match, not the first one, $output[0].

Saturday, September 8, 2007

What are the different functions in sorting an array?

Array sorting functions in PHP:
asort()
arsort()
ksort()
krsort()
uksort()
sort()
natsort()
rsort()

Sunday, August 12, 2007

How To Read the Entire File into a Single String?

If you have a file, and you want to read the entire file into a single string, you can use the file_get_contents() function. It opens the specified file, reads all characters in the file, and returns them in a single string.

Friday, August 3, 2007

How To Turn On the Session Support?

The session support can be turned on automatically at the site level, or manually in each PHP page script:
* Turning on session support automatically at the site level: Set session.auto_start = 1 in php.ini.
* Turning on session support manually in each page script: Call session_start() function.

Friday, June 22, 2007

How can we create a database using PHP and MySQL ?

We can create MySQL database with the use of mysql_create_db($databaseName) to create a database.

Wednesday, June 13, 2007

How can I execute a PHP script using command line?

Just run the PHP CLI (Command Line Interface) program and provide the PHP script file name as the command line argument. For example, "php myScript.php", assuming "php" is the command to invoke the CLI program.
Be aware that if your PHP script was written for the Web CGI interface, it may not execute properly in command line environment.