Wednesday, July 29, 2009

extracting substring

lets see a zend mock question regarding extracting substring.

Question:

Given the string:
$var = "john@php.net";
Which of the following will extract the TLD (top level domain) of ".net" from the string?


Answer:

1. strstr($var, strpos($var, "."));
the second parameter of strstr is the needle.

2. substr($var, strpos($var, "@"));
3. substr($var, strstr($var, "."));
the second parameter of substr is the offset.
4. substr($var, strpos($var, ".") + 1);
it will extract net.
5. substr($var, strpos($var, "."));
it will extract .net
See the book OpenCart 1.4 Template Design Cookbook.
See the book Joomla Mobile Development Beginners Guide

2 comments:

Anonymous said...

it`s
5. substr($var, strpos($var, "."));
".net" not "net"

sAm said...

It should be
substr($var, strpos($var, "."));

Please correct.