PHP preg_replace
Replace every run of digits before showing a booking record on a shared display.
A simple example
preg_replace
- Start with "a1b2"
- Do this Replace each digit with *
- Result "a*b*"
Syntax
preg_replace("/[0-9]+/", "*", $value)
Run these examples in PHP
Save one example at a time as lesson.php. Use PHP 8.2 or later.
php lesson.phpHide both ticket numbers
A service desk displays an identifier where bystanders could see it. Replace the intended numeric field or digit runs while preserving the rest of the record for the operator.
<?php
$value = "A12 / B304";
echo (preg_replace("/[0-9]+/", "*", $value)), PHP_EOL;
A* / B*
Both runs become one asterisk each.
Leave a public route name alone
A transit editor changes selected text in a route while preserving its remaining stops or punctuation. Identify which occurrences belong to the requested edit before rebuilding the public route label.
<?php
$value = "North Gate";
echo (preg_replace("/[0-9]+/", "*", $value)), PHP_EOL;
North Gate
No digits means no substitution.
Mask a public job number
A noticeboard shows the department prefix and hides the serial. The editor replaces a matching run of digits with a single star before sharing the label. Each sample has one numeric run.
What to watch for
Each run of one or more digits is one match, so its original length is hidden.