Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: Check a string

  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Check a string

    How would I make a PHP script that would check a string inside of a input field whenever someone submits?

    I need to make sure a person is typing in the URL for a youtube video so they can set that Youtube video on the website. The only problem is I don't want people be able to put something in like 'http://google.com' and it submit it to the database.

    So what are the ways that I can check to make sure that the URL that the person is submitting contain 'http://youtube.com/'?

    Thanks,
    Jon W

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    254
    Thanks
    1
    Thanked 63 Times in 63 Posts
    preg_match() maybe?

  • #3
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I'm not to sure what function that I would have to use. Thats why I'm asking. lol

  • #4
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,043
    Thanks
    19
    Thanked 42 Times in 42 Posts
    Quote Originally Posted by shadowmaniac View Post
    He told you already, look at example 3 from the link.
    PHP Code:
    <?php
    // get host name from URL
    preg_match('@^(?:http://)?([^/]+)@i',
        
    "http://www.php.net/index.html"$matches);
    $host $matches[1];

    // get last two segments of host name
    preg_match('/[^.]+\.[^.]+$/'$host$matches);
    echo 
    "domain name is: {$matches[0]}\n";
    ?>

  • #5
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    So can you explain this code to me line for line?

    PHP Code:
    <?php 
    // get host name from URL 
    preg_match('@^(?:http://)?([^/]+)@i'
        
    "http://www.php.net/index.html"$matches); 
    $host $matches[1]; 

    // get last two segments of host name 
    preg_match('/[^.]+\.[^.]+$/'$host$matches); 
    echo 
    "domain name is: {$matches[0]}\n"
    ?>


    I'm not sure exactly how I would edit this. What does all of this mean here:
    PHP Code:
    preg_match('@^(?:http://)?([^/]+)@i'
    And here:
    PHP Code:
    preg_match('/[^.]+\.[^.]+$/'$host$matches
    Is all of that apart of the function, or does it define something?

    Thanks,
    Jon W

  • #6
    Regular Coder kokjj87's Avatar
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    279
    Thanks
    1
    Thanked 55 Times in 54 Posts
    This is call regular expression, it is not easy to explain, the best way is to read them up.
    http://ysomeya.hp.infoseek.co.jp/eng-quick_regex.html


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •