Page 1 of 1

Error when trying to check the syntax of a URL

Posted: Sat Nov 27, 2004 3:21 pm
by davidjwest
Hi, apologies if this has been asked before but I get this error
Warning: Unknown modifier '/' in /home/qhwslos/public_html/register.php on line 75
Relating to this bit of code:

Code: Select all

<?php
	if ($_POST['website'] != '' & !preg_match("/^(http|ftp):///", $_POST['website'])) {
		$_POST['website'] = 'http://'.$_POST['website'];
	}
?>
It simply looks at user input from a form to check if the URL they give is in a valid format.

Advice welcome!

[/php_man]

Posted: Sat Nov 27, 2004 3:30 pm
by rehfeld
you need to escape the / like so:

Code: Select all

<?php
<?php
    if ($_POST['website'] != '' && !preg_match("/^(http|ftp):\/\//", $_POST['website'])) {
        $_POST['website'] = 'http://'.$_POST['website'];
    }
?>
?>

also notice i changed your & to &&

& is a bitwise operator. if you dont know what that is, your should probably avoid using it.


its also generally a bad idea and habit to get into to change the value of superglobals such as $_POST

you should do like this

Code: Select all

<?php
<?php
    if ($_POST['website'] != '' && !preg_match("/^(http|ftp):\/\//", $_POST['website'])) {
        $website = 'http://'.$_POST['website'];
    }
?>
?>