...

View Full Version : Check a string



Jon W
03-11-2009, 11:23 PM
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

shadowmaniac
03-11-2009, 11:37 PM
preg_match() (http://us.php.net/manual/en/function.preg-match.php) maybe?

Jon W
03-11-2009, 11:46 PM
I'm not to sure what function that I would have to use. Thats why I'm asking. lol

rafiki
03-12-2009, 12:40 AM
preg_match() (http://us.php.net/manual/en/function.preg-match.php) maybe?

He told you already, look at example 3 from the link.

<?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";
?>

Jon W
03-12-2009, 02:54 AM
So can you explain this code to me line for line?



<?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:
preg_match('@^(?:http://)?([^/]+)@i',

And here:
preg_match('/[^.]+\.[^.]+$/', $host, $matches)

Is all of that apart of the function, or does it define something?

Thanks,
Jon W

kokjj87
03-12-2009, 12:22 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum