Karan
Karan

Reputation: 267

Determining URL or String PHP

I'm making a link and text service, but I have a problem, which is: there is only 1 input text form, and the user could paste something like this: http:// asdf .com - which would register as a link, or 'asdf http:// test .com' because of the http://, it would register as a url, or asdf - which would register as a string, because it doesn't contain http://

BUT my problem arises when the user writes something like: asdf http://asdf.com, which in my current program outputs a "url" value. I've been experimenting for about an hour now, and I've got 3 bits of code (they were all in the same document being commented, so forgive me if they give errors!)

    <?
    $str = $_POST['paste'];
    if(stristr($str, "http://")) {
        $type = "url"; 
    }
    if(stristr($str, "https://")) { 
        $type = "url";
    }
    if($type!="url") { 
        $type = "string";
    } 
    ?>

Next:

<?
    $type = "url";
        if($type=="url"){
        $t = substr($str, 8);
        if(stristr($t, "https://")==$t){
        $type = "url";}
        if(stristr($t, "https://")==$t){ 
        $type = "url";}
        if(stristr($t, "http://")!=$t){
        $type = "string";}
        if(stristr($t, "https://")!=$t){
        $type = "string";}
        } 
        echo $type;
        ?>

Next:

<?    
$url = "hasttp://cake.com";
    if(stristr($url, "http://")=="") {
    $type = "string"; } else { 
    $type = "url"; 
    $sus = 1;}
    if(stristr($url, "http://")==$url) { 
    $type = "url"; }
    if($sus==1) { 
    $r = substr($url, 7);
    if(stristr($r,"http://")!="http://") { 
    $type = "url"; }
    if($r=="") {
    $type = "string";
    }
    }
    echo $type;
?>

I have no clue how I could go about classifying a string like 'asdf http://asdf.com' as a string, whilst classifying 'asdf' as a string, and classifying 'http://asdf.com' as a url.. Another idea I haven't tried yet is strpos, but that's what I'm working on now.

Any ideas?

Thanks alot! :)

Some parts of this question are getting cut off for some reason, apologies!

Upvotes: 0

Views: 646

Answers (5)

Mark
Mark

Reputation: 502

You should use a regular expression to check if the string starts with http

if(preg_match('/^http/',$string_to_check)){ //this is a url }

Upvotes: 0

corretge
corretge

Reputation: 1759

PHP parse_url is your function:

On seriously malformed URLs, parse_url() may return FALSE.

If the component parameter is omitted, an associative array is returned. At least one element will be present within the array. Potential keys within this array are:

  • scheme - e.g. http
  • host
  • port
  • user
  • pass
  • path
  • query - after the question mark ?
  • fragment - after the hashmark #

If the component parameter is specified, parse_url() returns a string (or an integer, in the case of PHP_URL_PORT) instead of an array. If the requested component doesn't exist within the given URL, NULL will be returned.

Upvotes: 1

Xyz
Xyz

Reputation: 6013

$type = strpos($str, 'http') === 0 ? 'url' : 'string':

The strpos function returns the position of a match within a string or FALSE if no match. The tripple equals checks that the result does not only translates to 0 (as FALSE would have done), but that it is in fact integer as well (i.e., the string begins with http).

You could also use something like

switch (true) {
    case strpos(trim($str), 'http://') === 0: 
    case strpos(trim($str), 'https://') === 0:
        $type = 'url'; 
        break;
    default:
        $type = 'string';
        break; // I know this is not needed, but it is pretty :-)
}

Upvotes: 0

Shef
Shef

Reputation: 45599

$type = '';
if (preg_match('%^https?://[^\s]+$%', $url)) {
    $type = 'url';
} else {
    $type = 'string';
}

This will match any value which starts with http:// or https://, and does not contain any space in it as type url. If the value does not start with http:// or https://, or it contains a space in it, it will be type string.

Upvotes: 4

JNDPNT
JNDPNT

Reputation: 7465

If I'm understanding the problem correctly you want to detect when the user inputs both a string and a url and parse each of them correspondingly.

Try using explode(" ", $userInput);, this will return an array containing all strings separated by a space. Than you can check that for each element in the array and set the type.

Upvotes: 0

Related Questions