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 9 of 9
  1. #1
    Regular Coder d'Anconia's Avatar
    Join Date
    Jan 2010
    Location
    Tempe, AZ
    Posts
    149
    Thanks
    16
    Thanked 6 Times in 6 Posts

    Problem with preg_match and delimiters

    Okay so I am making a form handler that checks that an entry is a valid website. Here is the code for this portion:

    PHP Code:
    $website_pattern "^([a-z0-9-]+(\.[a-z0-9-]+)+([/?].*)?$)^";
                                            
                        if (isset(
    $_POST['website']) && preg_match($website_pattern$_POST['website'])) {
                                
    $ws mysqli_real_escape_string ($dbc$trimmed['website']);}
                            else {echo 
    '<p class="error">Please enter a valid website url.</p>';} 
    And this has been giving me the following error:

    An error occurred in script '/home/kylhur/danconia.us/submit.php' on line 44: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash

    I tried applying preg_quote to the $website_pattern but that did not seem to work correctly. The following line is toward the bottom of the error reporting:

    [website_pattern] => \^\(\[a-z0-9-\]\+\(\\\.\[a-z0-9-\]\+\)\+\(\[/\?\]\.\*\)\?\$\)\^

    It looks like it is already trying to escape the variable with backslashes. Where am I going wrong with this?

    And also as a follow-up I eventually want the preg_match to also disqualify anything that starts with "http" (since this will be concatenated to the variable before being added to the database). But I gotta get this code to work first...

    Any help would be appreciated!
    Datagonia Web (My Portfolio)

    Powerful ideas for all lovers of personal and political freedom:
    Freedomain Radio
    Free Talk Live

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    you could just use a different delimiter, e.g. #, §, @
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Regular Coder d'Anconia's Avatar
    Join Date
    Jan 2010
    Location
    Tempe, AZ
    Posts
    149
    Thanks
    16
    Thanked 6 Times in 6 Posts
    But how would I go about doing that? And how would the preg_match function know that I was using @ as a delimiter? I lifted the PERL expression from someone else online who was using it to validate website URL's.

    When I tried using preg_quote($string, "delimiter"); it gave me the same error. How would preg_match know that @ was a delimiter and that I was not, in fact, searching for something possibly with an @ in the string.
    Datagonia Web (My Portfolio)

    Powerful ideas for all lovers of personal and political freedom:
    Freedomain Radio
    Free Talk Live

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    That's why you choose the delimiter you want. @ may be in a match, but I wouldn't recommend using ^ as delimiters since they carry start of subject or not including context in pcre. Escape the characters as necessary with a preg_quote. Use ~ if you want.
    On a side note, this pattern works fine. Won't be able to add beginning of subject now though.

    Edit:
    BTW, preg_quote is not required here. There is no "word" types that are being searched for and no dynamic search content, so therefore preg_quote is not required to escape.

  • #5
    Regular Coder d'Anconia's Avatar
    Join Date
    Jan 2010
    Location
    Tempe, AZ
    Posts
    149
    Thanks
    16
    Thanked 6 Times in 6 Posts
    Okay, the following is what I ended up using:
    $website_pattern = "%^([a-z0-9-]+(\.[a-z0-9-]+)+([/?].*)?$)%i";

    I guess I don't still fully understand the whole Perl Compatible Regular Expressions syntax. I guess the % character was used as a delimiter but what would have happened if I wanted to match that a string had a percentage character (%) in it? I would have to escape it or make it literal? Heck I still get confused as to which character is a slash vs. backslash and what the difference between them is.

    Oh well I guess I will have to read up on it some time soon. I've avoided the subject for long enough but for good searching and form handling I will have to eventually take the plunge. Thank you for your help!
    Datagonia Web (My Portfolio)

    Powerful ideas for all lovers of personal and political freedom:
    Freedomain Radio
    Free Talk Live

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    The string in question is of no relevance. Only if you place the delimiter characters into the pattern do they need escaping:
    PHP Code:
    $pattern '/([^\/]*\/?)+/'
    For example. Why do that when you can just change the delimiters:
    PHP Code:
    $pattern '~([^/]*/?)+~'
    You can use that pattern to match ~/path/to/something for example.

    Patterns like emails often use @. The pure reason is that the @ is a required part of the pattern in question, so its hard coded in and needs a single escape. Most of the other common delimiters are available in a typical dns, so with fine grain matches there would be a lot of escaping otherwise.

  • #7
    Regular Coder d'Anconia's Avatar
    Join Date
    Jan 2010
    Location
    Tempe, AZ
    Posts
    149
    Thanks
    16
    Thanked 6 Times in 6 Posts
    Okay I think I was getting confused between the term "delimiter" and escape characters. Thank you again for the help!
    Datagonia Web (My Portfolio)

    Powerful ideas for all lovers of personal and political freedom:
    Freedomain Radio
    Free Talk Live

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,460
    Thanks
    0
    Thanked 632 Times in 622 Posts
    The most commonly used delimiter is /

    As long as your pattern doesn't contain any slash characters there is no reason to consider using any other delimiter. It also makes it easier to grab the expression to use in JavaScript as JavaScript requires the / delimiter as it doesn't wrap it in "".
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by felgall View Post
    The most commonly used delimiter is /

    As long as your pattern doesn't contain any slash characters there is no reason to consider using any other delimiter. It also makes it easier to grab the expression to use in JavaScript as JavaScript requires the / delimiter as it doesn't wrap it in "".
    This I agree with. I typically use / exclusively. I will escape the patterned /'s up to about 4 before I opt for a different delimiter.
    I typically delimit in the order of /, #, ~, and finally @. The only ones I don't recommend are characters that contain special meaning, such as the ^, $, -, *, etc. Needless to say, I don't remember when I've used anything but /.


  •  

    Tags for this Thread

    Posting Permissions

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