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 2 of 2
  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Some PHP-Regex Questions

    Below is a PHP Regex that someone helped me create...

    PHP Code:
        if (preg_match_all('/\\{url=([a-zA-Z0-9-]+)\\}/'$body$matchesPREG_SET_ORDER) !== false){ 

    Questions:
    1.) Why are there double back-slashes before the left and right braces?

    2.) How would I change this to say, "Match all instances where the vakue in between the Curly Braces is a Lower-Case Letter, Upper-Case Letter, Number, or Hyphen and is between 2 and 20 characters in length"??

    3.) How would I modify this Regex to look for references to images in my "Article Body", where an Image could be either a PNG, JPEG, JPG or GIF?

    Here is my best guess...
    PHP Code:
        if (preg_match_all('/\\{img=([a-zA-Z0-9-]+)[.png|.jpeg|.jpg|.gif]\\}/'$body$matchesPREG_SET_ORDER) !== false){ 
    Thanks,


    Debbie

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,393
    Thanks
    13
    Thanked 353 Times in 349 Posts
    1) backslashes are escape characters. if you want a literal backslash, you have to escape that one too

    2) {2,20}

    3) does your guess work?
    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


  •  

    Posting Permissions

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