Home Products Downloads News Store Support

Stan's Blog

Xojo NthField() and CountFields() functions in PHP

How to use Maxprog products Maxprog's Blog

Compatible with MS Windows Compatible with MacOS

Xojo NthField() and CountFields() functions in PHP


In Xojo there is a handy function I use quite a lot for getting substrings between given delimiters from a piece of text, it is:

NthField(text, delimiter, fieldnumber)

Imagine you have the following text "1/2/3/4", NthField( "1/2/3/4", "/", 3 ) will return 3, that is the third substring between the "/" delimiters. It is that simple! This function works great in Xojo as long as the text is not too big. A few years ago I was porting a Xojo function to PHP that used NthField() so I decided to port the whole function. This is the PHP equivalent:

function nthfield($text, $delimiter, $fieldnumber) {
  $data = explode($delimiter, $text . $delimiter );
  return $data[$fieldnumber-1];

It is that simple, now you have the exact same NthField() in PHP. That function is fully accurate, I have been using it everywhere in my site for years.

Next I needed the related function to count the number of fields for a given delimiter:

CountFields(text, delimiter)

For the sample text we used above, "1/2/3/4", CountFields( "1/2/3/4", "/" ) would return 4, that is, the number of fields enclosed by the "/" delimiter. This is the PHP equivalent:

function countfields($text, $delimiter) {
  return count(explode($delimiter, $text));

As you can see the both functions use simple PHP array functions and since NthField() is slow in Xojo when the text is big, it would be simple to rewrite it :-)

Stan Busk - Software Engineer

Stan Busk Stan Busk
January 14, 2019

Recent questions from our users
  Mail rejected as SPAM UPDATED Screenshot
  How do I enter turboSMTP settings into MaxBulk Mailer YouTube Video
  How I can change the old activation key with the new one? YouTube Video
  Why do I get a Delivery Report message after each delivery? YouTube Video
  Setting DKIM on MaxBulk Mailer YouTube Video
  How do I add an Unsubscribe link to my messages
  How to import a Bank statement into iCash YouTube Video
  Is Maxprog software prepared for macOS 11 Big Sur? Screenshot
  How to transfer email lists between MaxBulk Mailer copies Screenshot
  How can I download files with sequential numbers in their name? YouTube Video
  Images are missing in the message preview Screenshot
  How to send an email with a picture in the body YouTube Video
  How can I send to a big list of emails with MaxBulk Mailer YouTube Video
  Mail-merge sender data using tags YouTube Video
  I set up my bulk email message but authentication fails YouTube Video

  [Valid RSS]  Subscribe to Maxprog's Knowledge Base

▾ Last videos, Online events and Q&A Sessions ▾

▾ Last customer reviews ▾

Did you like this post?

🔐 Site secured by Comodo SSL
🟢 UptimeRobot - Site Loaded properly