How can regular expressions be used in PHP to extract language-specific text from templates?

Regular expressions can be used in PHP to extract language-specific text from templates by matching the specific patterns of the text you want to extract. You can use regex functions like preg_match() or preg_match_all() to search for and extract the desired text based on the language-specific patterns in the templates.

// Sample template with language-specific text
$template = "Hello {en:World}! Hola {es:Mundo}! Bonjour {fr:Monde}!";

// Extract English text
preg_match('/{en:(.*?)}/', $template, $matches);
$englishText = $matches[1];

// Extract Spanish text
preg_match('/{es:(.*?)}/', $template, $matches);
$spanishText = $matches[1];

// Extract French text
preg_match('/{fr:(.*?)}/', $template, $matches);
$frenchText = $matches[1];

echo "English: " . $englishText . "\n";
echo "Spanish: " . $spanishText . "\n";
echo "French: " . $frenchText . "\n";