How can I break an url and store the key-words into database using php

444 Views Asked by At

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.

3

There are 3 best solutions below

0
On

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
1
On

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.

4
On

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