Results 1 to 4 of 4

Thread: '[' causing problems in php

  1. #1
    Registered
    Join Date
    Jun 2007
    Posts
    14

    '[' causing problems in php

    i keep getting errors from this line of code:
    PHP Code:
    ereg("[url"$comment
    this is the error: Warning: ereg() [function.ereg]: REG_EBRACK in /home/mobile/public_html/addcomment.php

    however this works:
    PHP Code:
    ereg("http"$comment
    so i'm inclined to think it's the '[' that's causing the problems. any idea how i can fix this?

    thanks!

  2. #2
    Registered Xander's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    263
    [ like many other characters needs escaping when using in double quotes ", you can escape it or alternatively use single quotes ' i.e.:

    Code:
    ereg("[url", $comment)

  3. #3
    Going strong! Kings's Avatar
    Join Date
    Aug 2003
    Posts
    61
    I've never used the ereg() function, but I regularly use the preg_match() function (which is almost the same), and the '[' character must be escaped, because it denotes the start of a character group.

    I'm not entirely sure how you do that with ereg(), but with preg_match() you escape it with the '\' character.

    I recommend you use the preg_match() function, as I believe that's faster and much more widely supported.
    Dennis Pallett

  4. #4
    Registered
    Join Date
    Apr 2006
    Location
    Michigan
    Posts
    99
    You definately don't want to use ereg.

    preg_match has a corresponding function for escaping special characters.

    PHP Code:
    $delimiter '/';
    $pattern $delimiter preg_quote('[url'$delimiter) . $delimiter;
    $result preg_match($pattern$comment$matches); 
    However, if you're just searching for simple strings, its easier to use str_pos:

    PHP Code:
    $position strpos($comment'[url'); 
    Watch out for the difference between a FALSE and a 0 return value from strpos. Use === FALSE or is_int to compare the result of strpos to determine if the string was found.

Similar Threads

  1. Review: Professional PHP Programming
    By Chris in forum Books
    Replies: 6
    Last Post: 07-17-2013, 05:26 AM
  2. Replies: 0
    Last Post: 05-24-2007, 04:48 AM
  3. Using PHP to produce PHP really screws with the head.
    By KLB in forum Website Programming & Databases
    Replies: 8
    Last Post: 02-14-2007, 11:36 AM
  4. AWS PHP script problems
    By tempyyyst in forum Advertising & Affiliate Programs
    Replies: 8
    Last Post: 04-04-2004, 07:12 AM
  5. php script problems
    By wrigh_g in forum HTML, CSS, Layout, and Design
    Replies: 4
    Last Post: 01-24-2004, 06:54 AM

Bookmarks

Posting Permissions

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