Initial idea:
I used a small text, but this fits perfectly for a full HTML page. It follows a sketch of what you can do, in a very simple way:
$oa = ( $sexo == "feminino" )? 'a':'o';
$texto = "Olá, car$oa cliente!\n";
$texto .= "Seja bem-vind$oa à nossa central de atendimento!\n";
See working on IDEONE.
If you need to change several words (it is 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 have how to make a replace. Just leave a special marking in 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 substitution after the text already exists. In the previous ones, we merge the text after defining the variables.
The same technique can be used for upper and lower case, just 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 from a array associative the desired words:
function substituir( $texto, $arr ) {
foreach ($arr as $key => $value) {
$texto = str_replace ( $key, $value, $texto);
}
return $texto;
}
Example of use:
$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 on IDEONE.
Using what PHP already has "embedded":
This syntax is more confusing for long lists of words, but the str_replace
PHP 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 );
Again, see working on IDEONE.
Notes:
In the first examples, we are using PHP’s own variable substitution.
In the second case, I used the symbols #oa#
and #hm#
, similar to the suggestion that the user @Sançao made to us comments.
I used the character #
, but it could be anything else that makes your job easier, as long as it’s a string that does not match any actual text information that should not be replaced. If we used [batata]
and [pipoca]
, would still.
In principle, no need for multibyte functions. If you use the same encoding on $texto
and the values of array, the encoding is irrelevant, because the literal search will always work.
you can use a placeholder defined with, for example, two brackets:
$texto = "Loren ipsum [[o]] cliente"
, utilisestr_replace('[[o]]', mudasexo($sexo), $texto)
andstr_replace('[[O]]', mudasexocaps($sexo), $texto)
– Pedro Sanção
Use a view. It’s easier! See my answer ;)
– Wallace Maxters