Error when trying to check the syntax of a URL

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
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

Error when trying to check the syntax of a URL

Post 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]
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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'];
    }
?>
?>
Post Reply