开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜