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
https://forums.phpfreaks.com/topic/178179-solved-hi-explode-and-strstr/
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.

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.