#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Posts
    22
    Rep Power
    0

    Preg_replace regex


    I've been fooling off with preg_replace because I don't have a clear understanding of the regex. I've read php.net etc but I can't find some of the unique identifiers that I see other people using like i/.

    I remembered an old script I gave up on and though since it was similar I should give it another shot.

    Code:
    $geocode	= file_get_contents("http://maps.google.com/maps/geo?q=". 
    				$address_url . '&key=' . $google_key);  	
    
    $g_pattern	= '/"PostalCodeNumber" : "(.*?)"/';
    
    preg_match($g_pattern, $geocode, $zip);
    This is basically pulling a text file from google maps to find the zip code. I don't really want to parse the xml and so I thought preg_match would be much simpler. haha for me, I suck at regex!
    The above result for $zip is "1", which means I have a bit further to go. How do I fix this or do you know of any unique identifiers, maybe a detailed library?

    This is an example address I pulled off Google to show you what data is being looked at: Example You'll need to click "go" or press [Enter] in your web-browsers url line for the feed to show up.
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,556
    Rep Power
    2338
    Looks like JSON, so perhaps you can use json_decode to handle it?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Posts
    22
    Rep Power
    0
    That is a great idea! I'll use it! But I still want to learn a little non-standard regex =]
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    If it's JSON, use it like JSON. As a wise man once said:
    “Some people, when confronted with a problem, think 'I know, I’ll use regular expressions!' Now they have two problems.” Regular expressions are very difficult to understand.

    That being said, nobody can help you without a section of the text to match against. All I can tell you is that what you've given is a valid regular expression.

    I can also tell you we have a regular expression forum.

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo