Jump to content

[SOLVED] The best way to extract data from a string.


elite_prodigy

Recommended Posts

Basically, I need to get a series 0f domains out of a string and store them in an array where they will then be cross-checked on another array, but the details after I get the domain are not nearly as important as getting the domain itself.

 

I've included a sample of what I would have to extract. (Posting the real data would feel too much like advertising to me and would thus feel tacky.)

 

sub.domain.com: username
abc.alphabet.com: qwerty
example.com: bob
google.com: qwerty
mydomain.org: jose
cia.gov: obama
*: root

 

I don't care about anything after the ":" (colon), of course I could explode() the string, and cycle through the array and add every other index to the new array, but that just seems messy to me. I was wondering if there is a function I've missed, or if I'm over-thinking things.

 

I don't expect anyone to write the function for me, I won't stop you if you want to because I can't, an example would be nice, but a simple explanation of the method to use would be awesome and highly apreciated.

 

-David

 

(As always, you guys truly do rock!)

a bunch of ways to do this

 

strpos & substr

  find the colon, and grab everything before it

 

preg_match, wud match the pattern of yer query, but it creates a new set of arrays

I believe the pattern for preg match wud be '@(.*?)\:.*@'

 

but the strpos method is faster :)

 

Hope that helps

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.