Jump to content

[SOLVED] Hi, explode and strstr.


ted_chou12

Recommended Posts

Hi, I seem to be confused about the strstr function, eg. i have a string like:

"a.b.c.d.e.f"

"a.b.c.d.e"

I always want it to split at the second "." from the end, so resulting in:

"e.f"

"d.e" respectively

etc...

Please feel free to ask me if you don't understand.

Thanks,

Link to comment
Share on other sites

Is this for processing domain names?  strstr() cannot choose the second "." from the end.  It matches simple string patterns only.

 

An alternative is to explode() on ".", count() the number of items in the array, take the last two items from the array and implode() them again using ".".

 

Or you can explode(), reverse the array, take the first two items, reverse again and then use implode().  That's not as fast but some people like that approach because there's no arithmetic :)

 

Another approach is using a regexp, which is able to specify rules like "take the last two items seperated by dots".

 

Also if you are processing domain names, be aware of issues with country level domain names.  Some are two levels and some are three levels, and some countries even mix both two and three level.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.