could you please help me on this problem I want in PHP to replace the begining and the end of a string if match exists, as example:
source strings :
tr_abcdef_lang
or tr_abcdef
or tr_abcdef_cba
I want to replace the string to have it like this:
fk_abcdef
or fk_abcdef_cba
I mean if strings ends with _lang
or _language
to remove it, and at the begining replace everything before the first _
with fk
.
So more examples :
tr_abcdef => fk_abcdef
tr_abcdef_language => fk_abcdef
x_abc_cba => fk_abc_cba
x_abc_cba_lang => fk_abc_cba
t_tablename_languages => fk_tablename
i solve your regex problem
$pattern = "/^([a-zA-Z]+?)_(\w+?)([_trans|_translation|_languages]*?)$/i";
$replacement = 'fk_$2';
$fk_field_name = preg_replace($pattern,$replacement, "x_abc_cba_gsd_lang");
echo $fk_field_name;
my code
Try this ~(.*?)_(.*)_(.*)~si
preg_match_all( '~(.*?)_(.*)_(.*)~si', "x_abc_cba",$M);
$E='';
if($M[3][0]!="lang" and $M[3][0]!="language" and $M[3][0]!="languages"){
$E="_".$M[3][0];
}
echo ("fk_".$M[2][0].$E);
or try it without php regular expression
$String="x_abc_cba_lang";
$xp=explode("_",$String);
$xp[0]="fk";
end($xp);
$Ek=key($xp);
if($xp[$Ek]=="lang" or $xp[$Ek]=="language" or $xp[$Ek]=="languages"){
unset($xp[$Ek]);
}
echo implode("_",$xp);
Thanks for all who tried to help
i found the solutions and it looks like:
$pattern = "/^([a-zA-Z]+?)_(\w+?)(_trans|_translation|_languages)?$/i";
$replacement = 'fk_$2';
$fk_field_name = preg_replace($pattern, $replacement, $tbl_name);
Thanks again and best regards
Wael
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.