Showing posts with label strings. Show all posts
Showing posts with label strings. 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

substr - related questions

What is substr in php?

substr is used to return a part of a string

Two arguments can be passed to the substr

1. start value
2. length value

Examples
1. start value

substr('interview',1); // returns "nterview"
substr('interview',-2); //returns "ew"

2. Length value

substr('interview',-2,1); //the output is "e"
substr('interview',-3,-2); //the output is "i"
substr('interview',1,2); //the out put is "nt"

Your comment always welcome

Thursday, April 1, 2010

what is the output $$b

if $a=5 and $b='a'

what is the value of $$b

value of $$b is 5

what is the difference between echo-print functions

Simple!!

echoOutput one or more strings

print Output a string

echo and print -- print() is not actually a real function (it is a language construct)

echo statement will not return anyrhing

print will return false if it fails

most of the peoples say echo is more efficient that print

There are so many php interview questions and answers will keep coming in this wesite