Skip to content

Instantly share code, notes, and snippets.

@Dimasmagadan
Created May 22, 2023 11:02
Show Gist options
  • Save Dimasmagadan/e78b17e5dd6465f424ded15ddf7d75cf to your computer and use it in GitHub Desktop.
Save Dimasmagadan/e78b17e5dd6465f424ded15ddf7d75cf to your computer and use it in GitHub Desktop.
<?php
// Callback function to format numbers with &nbsp;
function format_numbers_with_nbsp($content) {
// Regular expression pattern to match numbers
$pattern = '/\b(\d{1,3}(?:\d{3})*)\b/';
// Replace matched numbers with formatted version
$formatted_content = preg_replace_callback($pattern, 'add_nbsp_to_number', $content);
return $formatted_content;
}
add_filter('the_content', 'format_numbers_with_nbsp');
// Callback function to add &nbsp; to numbers
function add_nbsp_to_number($matches) {
$number = $matches[0];
// Format the number by adding &nbsp;
$formatted_number = number_format($number, 0, '', '&nbsp;');
return $formatted_number;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment