Showing posts with label simple interview questions. Show all posts
Showing posts with label simple interview questions. Show all posts

Thursday, April 8, 2010

extract domain name from the url using regular expression

Question: How to extract domain name from a url using regular expressions

Answer:

preg_match('@^(?:http://)?([^/]+)@i',"http://blogger.com", $matches);
$host = $matches[1];

// get last two segments of host name
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "domain name is: {$matches[0]}\n";

The output is

domain name is: blogger.com

Tuesday, April 6, 2010

What is array in php

Question: What is array in php and how to create an array in php?

Answer: In php array is an ordered map. It contains values and keys.

To create an array we need to use array keyword

Example:

$a= array('php','interview','questions','answers');

This creates simple array.