开发者

PREG_REPLACE if string ends with char X, then the last X character replace it with a Y character

开发者 https://www.devze.com 2023-03-24 03:33 出处:网络
If a string ends with the character X, i want to replace that particul开发者_开发百科ar character with the character Y.

If a string ends with the character X, i want to replace that particul开发者_开发百科ar character with the character Y.

Example 1

anything_number_alphabet_dashes_anythingX[END OF STRING]

to

anything_number_alphabet_dashes_anythingY[END OF STRING]

Example 2

DontReplaceThis_X_JustOnlyThisLast_X[END OF STRING]

to

DontReplaceThis_X_JustOnlyThisLast_Y[END OF STRING]

I want to do this in PHP with preg_replace or ereg_replace.


Dollar sign is the end of string anchor, so:

$new_str = preg_replace('/X$/', 'Y', $old_str);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号