views:

119

answers:

3

Like http:webmail.wipro.com#a:?b;

I want to break this url and store only webmail and wipro into my database. Can any one help me out with this please. Using php.

+5  A: 

You should use the parse_url function to retrieve the parts and then use at your will (in your case, saving them in database).

Here is a test code/output from the manual:

<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';

print_r(parse_url($url));

echo parse_url($url, PHP_URL_PATH);

Prints the following:

Array
(
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
)
/path
phpfour
+1  A: 

You should use regular expressions. If you run something like

preg_match('http:(.*?).(.*?).com#a:?b;', 'http:webmail.wipro.com#a:?b;', $matches);

$matches[1] should say webmail and $matches[2] should contain wipro.

There is more documentation for regexes and preg_match on the PHP site.

Scavenger
it can have any option like http://stackoverflow.com/questions/1879683/how-can-i-break-an-url-and-store-the-key-words-into-database-using-phpif this is the case i want to store stackoverflow,questions,break,store,words,databasethen how is it possible to use the same query
sangeetha
A: 

It sounds like what you're looking for is to recognise any words at all within the URL. In this case, try this RegExp:

preg_match_all ('/\b(\w{4,})\b/', $url, $matches);

$matches will contain an array of all word-like strings of length 4 or more

K Prime
<?php$url="http://webmail.wipro.com";preg_match_all ('/\b(\w{4,})\b/', $url, $matches);$n=count($matches);echo $n; for($i = 0; $i < $n; $i++){ echo "Piece $i = $matches[$i] <br />"; }?>out put:2Piece 0 = ArrayPiece 1 = Array
sangeetha
why am i getting such output plz help me out
sangeetha
can any one help me out
sangeetha
$matches is not containing such words of length 4
sangeetha
`preg_match_all` returns subarrays of inner matches as well (ie. the `..(\w{4,0})..`). The words you want are in `$matches[1]`.
K Prime