Initial idea:
I used a little text, but this is fine for a full HTML page. Here's an outline of what you can do, quite simply:
$oa = ( $sexo == "feminino" )? 'a':'o';
$texto = "Olá, car$oa cliente!\n";
$texto .= "Seja bem-vind$oa à nossa central de atendimento!\n";
See working at IDEONE .
If you need to change multiple words (case sensitive):
if ( $sexo == "feminino" ) {
$oa = 'a';
$hm = 'mulheres';
} else {
$oa = 'o';
$hm = 'homens';
}
$texto = "Olá, car$oa cliente!\n";
$texto .= "Seja bem-vind$oa à nossa central de atendimento para $hm!\n";
If the text comes ready, from another place or template, you can do a replace . Just leave a special mark on the original text:
$texto = "Olá, car#oa# cliente!\n";
$texto .= "Seja bem-vind#oa# à nossa central de atendimento para #hm#!\n";
if ( $sexo == 'feminino' ) {
$texto = str_replace ( '#oa#' , 'a' , $texto );
$texto = str_replace ( '#hm#' , 'mulheres', $texto );
} else {
$texto = str_replace ( '#oa#' , 'o' , $texto );
$texto = str_replace ( '#hm#' , 'homens', $texto );
}
Note that this time we do the replacement after the text already exists. In the previous ones, we merge the text after defining the variables.
The same technique can be used for uppercase and lowercase letters, you would have to define, for example #oa#
and #OA#
in the templates.
Applying the concepts in a more complete function:
With this function, we do the str_replace
in a loop , taking an associative array the desired words:
function substituir( $texto, $arr ) {
foreach ($arr as $key => $value) {
$texto = str_replace ( $key, $value, $texto);
}
return $texto;
}
Example usage:
$texto = "Olá, car#oa# cliente!\n";
$texto .= "Seja bem-vind#oa# à nossa central de atendimento para #hm#!\n";
$masculino = array( '#oa#' => 'o', '#hm#' => 'homens' /*, etc */ );
$feminino = array( '#oa#' => 'a', '#hm#' => 'mulheres' /*, etc */ );
$texto = substituir( $texto, $sexo == 'feminino' ? $feminino : $masculino );
See working at IDEONE .
Using what PHP already has "embedded":
This syntax is more confusing for long word lists, but PHP str_replace
also accepts arrays , eliminating the need for an extra function:
$texto = "Olá, car#oa# cliente!\n";
$texto .= "Seja bem-vind#oa# à nossa central de atendimento para #hm#!\n";
$placeholders = array( '#oa#','#hm#' /*, etc */ );
$masculino = array( 'o' ,'homens' /*, etc */ );
$feminino = array( 'a' ,'mulheres' /*, etc */ );
$texto = str_replace( $placeholders, $sexo=='feminino'?$feminino:$masculino, $texto );
Once again, check out IDEONE .
Notes:
-
In the first few examples, we are using PHP's own variable override.
-
In the second case, I used the symbols #oa#
and #hm#
, in a similar way to the suggestion that the user @ Sançao made in comments .
I used the #
character, but it can be anything else that makes your work easier, as long as it is a string that does not match some real text information that should not be replaced. If we used [batata]
and [pipoca]
, it would be the same.
-
In principle, no need for multibyte functions. If you use the same encoding in $texto
and in array values, encoding is irrelevant since the literal search will always work.
>