CountOccurrences

Summary: Count occurrences of a string in page source text, optionally limited to sections between markers
Version: 20260227
Prerequisites: PmWiki 2.2.0 or later, PHP 8.0 or later
Status: Stable
Maintainer: Holger Kremb
License: GPL-3.0-or-later
Categories: Markup PHP85
Users: (view / edit)


Compatibility

  • Tested with PHP 8.5.2
  • Last tested on PmWiki 2.5.5

Questions answered by this recipe

  • How can I count occurrences of a string in a wiki page?
  • How can I count occurrences only between two markers?
  • How can I reuse a counted value later on the same page?
  • How can I perform case-insensitive or whole-word searches?

Description

CountOccurrences provides a markup directive to count how often a given string (needle) appears in the source text of a wiki page.

The count can optionally be limited to the text between two markers and can be stored internally for later reuse within the same page (e.g. for conditions or summaries).

The recipe works on raw page source text; visual line wrapping is ignored.

Installation

  1. Download countoccurrences.zipΔ
  2. Copy countoccurrences.php into your cookbook/ directory
  3. Add the following line to config.php: include_once("$FarmD/cookbook/countoccurrences.php");

Configuration

This recipe does not require any configuration. All options are provided via markup parameters.

Internationalization

This recipe does not define any translatable strings.

Usage

Basic usage on the current page:

  (:countoccurrences needle=test:)

Count occurrences on another page:

  (:countoccurrences page=Group.Page needle=test:)

Case-insensitive search:

  (:countoccurrences needle=test ignorecase=1:)

Whole-word matching:

  (:countoccurrences needle=test wholeword=1:)

Limit counting to a section between markers:

  (:countoccurrences needle=test start="[[#Pm_A]]" end="[[#Pm_B]]":)

Store the result for later reuse on the same page:

  (:countoccurrences needle=test id=c01:)

Reuse the stored value without recounting:

  (:countoccurrences get=c01:)

Example condition:

  (:if expr ( (:countoccurrences get=c01:) ) > 10 :)
Too many occurrences.
(:ifend:)

Notes

  • Markers can be named freely.
  • Markers may be passed as to avoid anchor duplication; they map internally to #X.
  • If no matches are found, the result is 0.
  • The recipe operates on raw page source text, not rendered HTML.
  • Stored values should be accessed via get=… for reliable reuse within the same page.

To do / some day / maybe

  • Optional support for regular expressions (advanced mode).
  • Optional reporting of line numbers for matches (debug use).

Change log / Release notes

  • 2026-02-27 – Initial public release

See also

Contributors

  • Holger Kremb
  • Idea inspired by a request from nitram (PmWiki community)

Comments

See discussion at CountOccurrences-Talk

User notes : If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.