1

There's a French typographic rule to write properly some words in a genderless way, by adding an (·) interpunct between letters. A few authors on my website are however typing a simple (.) dot instead.

As a solution, I'd like to create a function to replace in PHP strings each dots which are placed between two lowercase letters by interpuncts. But my PHP skills are rather limited… Here is what I'm looking for:

REPLACE THIS:

$string = "T.N.T.: Chargé.e des livreur.se.s."

BY THIS:

$string = "T.N.T.: Chargé·e des livreur·se·s."

Could someone help me please? Thank you.

2 Answers2

3

Use the preg_replace with pattern to dynamically match 3 groups - two lowercase letters (including special French letters) and dot between, and use the first and third captured group in replacement, along with intepunct:

$string = "T.N.T.: Chargé.e des livreur.se.s.";
$pattern = '/([a-zàâçéèêëîïôûùüÿñæœ])(\.)([a-zàâçéèêëîïôûùüÿñæœ])/';
$replacement = '$1·$3'; //captured first and third group, and interpunct in the middle 

//results in "T.N.T.: Chargé·e des livreur·se·s."
$string_replaced = preg_replace($pattern, $replacement, $string); 

More about preg_replace:

https://www.php.net/manual/en/function.preg-replace.php

Nikola Kirincic
  • 3,651
  • 1
  • 24
  • 28
0

You could use str_replace() if you know the grammar rules surrounding the dots you want to replace. (for instance, if everything between éand e is concerned, then you can do :

$bodytag = str_replace("é.e", "é·e", $sourceText);

But you will always risk some side effects. For instance if there is an acronym you don't want to be replaced with this pattern. I don't think there is any magic way to avoid this.

More specifically

I'd like to create a function to replace in PHP strings each dots which are placed between two lowercase letters by interpuncts.

This can be achieved with preg_replace() and the appropriate REGEX

See this post

Altherius
  • 754
  • 7
  • 23