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

Thread: PHP Regex Help

  1. #1
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts

    PHP Regex Help

    I've never written a successful regex in my life and I'm starting to tinker with them now. I have a text file, the contents of which I am loading into a variable. The basic text file looks like a lot of lines like this:
    Code:
    "PRBUPort"="80"
    "PRBUPath"="/"
    "PRBUUrl"="http://mysite.com/"
    I then want to run through the variable and pull out all instances of "PRBUUrl"="<some url here>" so that I can capture all URLs passed in the text file for this particular recurring variable.

    I'm using this preg_match_all to capture each instance:
    PHP Code:
    preg_match_all('%"PRBUUrl"=".*"%',$text_1,$url_list_1); 
    That's working well enough, but I'd really like to actually exclude the "PRBUUrl"=" and " parts so that I get ONLY the part in the middle. The URL is not always an actual URL - it can have wildcard characters (*), IP addresses, etc. and other URLs actually appear in other places that I DON'T want to capture (so I can't just capture all URLs, for example).

    This is supposed to be pretty easy stuff but it's just not clicking for me... Any ideas?
    Last edited by Rowsdower!; 01-22-2010 at 11:14 PM.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    preg_match_all('%"PRBUUrl"="\K[^"]+%',$text_1,$url_list_1);
    Try that
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • Users who have thanked JAY6390 for this post:

    Rowsdower! (01-22-2010)

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Quote Originally Posted by JAY6390 View Post
    preg_match_all('%"PRBUUrl"="\K[^"]+%',$text_1,$url_list_1);
    Try that
    are you imitating the [PHP][/PHP]-BBCode's color pattern?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    lol I just copied and pasted the line, and edited the regex directly...the forum textbox kept the formatting for me...
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Quote Originally Posted by JAY6390 View Post
    lol I just copied and pasted the line, and edited the regex directly...the forum textbox kept the formatting for me...
    Oops. I use the other "Mode" of the editor, so I thought ...

    Edit: Feel bad to hijack the thread anymore
    Quote Originally Posted by JAY6390 View Post
    Also, if you don't want the forum to parse tags, you can just use the [noparse] tags around tags, and they'll stay intact no need to put fonts inside them
    I know, but that's a different story
    Last edited by abduraooft; 01-22-2010 at 05:24 PM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    I quite like the full version, especially the [ICODE][/ICODE] and [PHP][/PHP] buttons. Most other forums don't have this
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #7
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Also, if you don't want the forum to parse tags, you can just use the [noparse] tags around tags, and they'll stay intact no need to put fonts inside them
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #8
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by JAY6390 View Post
    preg_match_all('%"PRBUUrl"="\K[^"]+%',$text_1,$url_list_1);
    Try that
    Make it look easy, why don't you?!

    That worked perfectly, thanks! I need to spend some time studying the syntax because I was trying tons of stuff, but nothing even close to that.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #9
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    The syntax is very simple. Up until the \K it matches the text as you know. then the \K makes it forget everything before it (ie the text "PRBUUrl"="). Then the [^"]+ matches all the characters that follow that aren't the character " so once it reaches the closing " it stops matching and hey presto, you have the match perfect

    I've been using regex for years now and absolutely love working out regex's so play around with them constantly, so yes it comes pretty easy now. It wasn't at the beginning. Practice makes perfect as they say!
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)


  •  

    Posting Permissions

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