SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast ScubaDvr2's Avatar
    Join Date
    Jun 2006
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Opinions on my reg ex

    Hello,
    I haven't used regular expressions in a long time, but I needed them today so i built this:

    PHP Code:
    preg_match_all('{\[image:\s?(.[^,]*),?\s?(.[^,]*)?,?\s?(\d[^,]*)?,?\s?(\d*)?\]}si'$string$images); 
    This code is used so that it can match the syntax of [image: url, alt, width, height] and only url is required where the other three are optional.

    I had to glance through my regular expressions book to get this working. That leads me to one question. Is there something I can do to improve this?

    Thanks

  2. #2
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    preg_match_all('~^\[(image)\: (url),? (alt)?,? (width)?,? (height)?$~', $string, images);

    haven't tested it, don't know whether this fulfills your requirement or not.

  3. #3
    SitePoint Enthusiast ScubaDvr2's Avatar
    Join Date
    Jun 2006
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks I will try this.
    Did you give me this code because you thought mine wasn't very good? Is there an advantage to the code you gave me over mine?

    Thanks.

  4. #4
    SitePoint Enthusiast ScubaDvr2's Avatar
    Join Date
    Jun 2006
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh ok I see what you did. I wasn't clear enough. I was just using [image: url, alt, width, height] to show what the syntax is for what I want. I need it to parse something like this:

    [image: http://www.example.com/image.gif, This is a square image, 50, 50]

    Does that make sense?

    My current regex works but i just wanted to know if there is anything i can do to improve or optimize it. Thanks

  5. #5
    SitePoint Enthusiast ScubaDvr2's Avatar
    Join Date
    Jun 2006
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok no responses I guess that means my code is perfect. Thanks.

  6. #6
    SitePoint Member robbin.joe's Avatar
    Join Date
    Oct 2009
    Location
    Beijing, China
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think, for the url, alt, width, height, no need to use regex.

    such as {\[image.*?)\]}, and then get the matched string, explode by ",".


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
  •