preg_match: ensure the start and the end contains something

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

preg_match: ensure the start and the end contains something

Post by lauthiamkok »

Hi,

I want to have the regular expression that makes sure the beginning of the string contains 'http://' and '/' and the end.

This is a longer version I came up with,

Code: Select all

if(!preg_match("/(^http:\/\//", $site_http)) 
{
	$error = true;
	echo '<error elementid="site_http" message="site_http - Your link appears to be invalid. Please confirm that your link contains http:// at the start."/>';
}
elseif (!preg_match("/\/$/", $site_http)) 
{
	$error = true;
	echo '<error elementid="site_http" message="site_http - Your link appears to be invalid. Please confirm that your link has ended with a /."/>';
}
but I thought these two expressions can put together like below, but it wont work,

Code: Select all

if(!preg_match("/(^http:\/\/)&(\/$)/", $site_http)) 
{
	$error = true;
	echo '<error elementid="site_http" message="site_http - Your link appears to be invalid. Please confirm that your link contains http:// at the start and a / at the end."/>';
}
the multiple expressions that I try to combine must be wrong! any idea?

thanks,
Lau
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: preg_match: ensure the start and the end contains someth

Post by requinix »

No need for regular expressions.

Code: Select all

if (strncmp($site_http, "http://", 7) != 0 || $site_http[strlen($site_http) - 1] != "/") {
amargharat
Forum Commoner
Posts: 82
Joined: Wed Sep 16, 2009 2:43 am
Location: Mumbai, India
Contact:

Re: preg_match: ensure the start and the end contains someth

Post by amargharat »

Use following code,

Code: Select all

preg_match('/^http\:\/\/(.*?)\/$/', 'http://www.google.com/', $matches);
if($matches)
	echo "contains 'http://' at the start and '/' at the end";
else
	echo "no matches found";
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Re: preg_match: ensure the start and the end contains someth

Post by lauthiamkok »

tasairis wrote:No need for regular expressions.

Code: Select all

if (strncmp($site_http, "http://", 7) != 0 || $site_http[strlen($site_http) - 1] != "/") {
this is a good trick! thanks alot! :D
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Re: preg_match: ensure the start and the end contains someth

Post by lauthiamkok »

amargharat wrote:Use following code,

Code: Select all

preg_match('/^http\:\/\/(.*?)\/$/', 'http://www.google.com/', $matches);
if($matches)
	echo "contains 'http://' at the start and '/' at the end";
else
	echo "no matches found";
thanks so much for this! :D
Post Reply