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

Topics

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
at www.maxprog.com

Meet our new apps
AI support that never sleeps
Deploy an AI agent that learns your product, answers customers instantly, and lets one person handle what used to take an entire team. SoftDesk supports both email and chat with ready-to-install widgets.
SoftDesk
AI support that never sleeps
Learn more
Shared family finance made simple
Track income, expenses and everyday household spending in a simple, modern app designed for families, couples and shared budgets.
FamilyCash
Shared family finance made simple
Learn more
Fun learning that keeps kids engaged
Help children build skills through playful activities designed to make learning more enjoyable, motivating and rewarding.
KidBoost
Fun learning that keeps kids engaged
Learn more

 Recent questions from our users

  Como recuperar un archivo dañado NEW
  Des catégories qui disparaissent NEW
  How to reconcile several transactions at once NEW
  How can I Cc and/or Bcc a message with MaxBulk Mailer? NEW YouTube Video
  How to extract Contacts from Outlook Mac (OLM File) NEW Screenshot
  Does MaxBulk Mailer supports List-Unsubscribe ? Screenshot
  Trouble with mlm setup, 'The URL is not valid' error. Screenshot
  Problem with importing recipients Screenshot
  Why the iCash transaction amount column has disappeared Screenshot
  451 Error code Screenshot
  Problem sending email to Gmail recipients Screenshot
  What is the meaning of the recipient panel icons Screenshot
  How to create new projects in iCash Screenshot
  How to add transactions to iCash faster YouTube Video
  How to set BBEdit as the FTP Disk text editor?

 Last videos


🔐 Secured by Sectigo SSL | UptimeRobot - Site Loaded properly