Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9063167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:55:24+00:00 2026-06-16T15:55:24+00:00

Let’s say I have this string: <div>john doe is nice guy btw 8240 E.

  • 0

Let’s say I have this string:

<div>john doe is nice guy btw 8240 E. Marblehead Way 92808  is also</div>

or this string:

<div>sky being blue? in the world is true? 024 Brea Mall  Brea, California 92821 jackfroast nipping on the firehead</div>

How would I go about extracting the address from one of these strings? This would involve some sort of Regex, right?

I’ve tried looking online for a solution using JavaScript or PHP, but to no avail.
And no other post here on Stack Overflow (as far as I know) provides a solution that uses jQuery and/or Javascript and/or PHP. (The closest is Parse usable Street Address, City, State, Zip from a string, which DOESN’T have any code in the thread about extracting a postal code from a string.

Can somebody point me in the right direction? How would I go about accomplishing this in jQuery or JavaScript or PHP?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:55:25+00:00Added an answer on June 16, 2026 at 3:55 pm

    Tried this on twelve different strings that were similar to yours and it worked just fine:

    function str_to_address($context) { 
    
        $context_parts = array_reverse(explode(" ", $context)); 
        $zipKey = ""; 
        foreach($context_parts as $key=>$str) { 
            if(strlen($str)===5 && is_numeric($str)) { 
                $zipKey = $key;
                break; 
            }
        }
    
        $context_parts_cleaned = array_slice($context_parts, $zipKey); 
        $context_parts_normalized = array_reverse($context_parts_cleaned); 
        $houseNumberKey = ""; 
        foreach($context_parts_normalized as $key=>$str) { 
            if(strlen($str)>1 && strlen($str)<6 && is_numeric($str)) { 
                $houseNumberKey = $key;
                break; 
            }
        }
    
        $address_parts = array_slice($context_parts_normalized, $houseNumberKey);
        $string = implode(' ', $address_parts);
        return $string;
    }
    

    This assumes a house number of at least two digits, and no greater than six. This also assumes that the zip code isn’t in the “expanded” form (e.g. 12345-6789). However this can be easily modified to fit that format (regex would be a good option here, something like (\d{5}-\d{4}).

    But using regex for parsing user-inputted data… Not a good idea here, because we just don’t know what a user is going to input because there were (as one can assume) no validations.

    Walking through the code and logic, starting with creating the array from the context and grabbing the zip:

    // split the context (for example, a sentence) into an array, 
    // so we can loop through it. 
    // we reverse the array, as we're going to grab the zip first. 
    // why? we KNOW the zip is 5 characters long*.
    $context_parts = array_reverse(explode(" ", $context));  
    
    // we're going to store the array index of the zip code for later use 
    $zipKey = ""; 
    
    // foreach iterates over an object given the params, 
    // in this case it's like doing... 
    // for each value of $context_parts ($str), and each index ($key)
    foreach($context_parts as $key=>$str) { 
    
        // if $str is 5 chars long, and numeric... 
        // an incredibly lazy check for a zip code...
        if(strlen($str)===5 && is_numeric($str)) {  
            $zipKey = $key;
    
            // we have what we want, so we can leave the loop with break
            break; 
        }
    }
    

    Do some tidying so we have a better object to garb the house number from

    // remove junk from $context_array, since we don't 
    // need stuff after the zip
    $context_parts_cleaned = array_slice($context_parts, $zipKey); 
    
    // since the house number comes first, let's go back to the start
    $context_parts_normalized = array_reverse($context_parts_cleaned);
    

    And then let’s grab the house number, using the same basic logic that we did the zip code:

    $houseNumberKey = ""; 
    foreach($context_parts_normalized as $key=>$str) { 
        if(strlen($str)>1 && strlen($str)<6 && is_numeric($str)) { 
            $houseNumberKey = $key;
            break; 
        }
    }
    
    // we probably have the parts we for the address.
    // let's do some more cleaning 
    $address_parts = array_slice($context_parts_normalized, $houseNumberKey);
    
    // and build the string again, from the address
    $string = implode(' ', $address_parts);
    
    // and return the string
    return $string;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say I have this element for displaying the website logo: <div id="web-title"> <a
Let me frame it this way.. Say I have an application server running on
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have some text as follows: do this, do that, then this,
let's say I have the following string: string s = A B C D
let's say.. I have the following java bean. Case1: (Student Bean) Integer id; String
Let's say I got this string: $str = alemylaife; (I know it's misspelled, all
Let's say I have this interface: // .h @interface DataObject : NSObject { NSString*
Let's say that I have classes like this: public class Parent { public int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.