islambzh/string_replace

此包包含一个特质文件,允许您根据提供的数据在字符串中替换值。

dev-main 2023-12-07 04:59 UTC

This package is auto-updated.

Last update: 2024-09-07 06:30:14 UTC


README

描述

此包包含一个特质文件,允许您根据提供的数据在字符串中替换值。

示例

echo self::stringReplace('Hello, :name! There are :time left until the new year', [
    'name' => 'John',
    'time' => '11 days'
]);
// Hello, John! There are 11 days left until the new year

但这并不很有趣,对吧?让我们来否定日子吧!

$text = 'Hello, :name! There are :time :time[{1}day|days] left until the new year';
echo self::stringReplace($text, [
'name' => 'John',
'time' => 1
]);
// Hello, John! There are 1 day left until the new year
echo self::stringReplace($text, [
'name' => 'John',
'time' => 2
]);
// Hello, John! There are 2 days left until the new year
echo self::stringReplace($text, [
'name' => 'John',
'time' => 23
]);
// Hello, John! There are 23 days left until the new year

如果您决定“编程”文本...

$text = 'You have :count :count[tries|try|tries] left!'
    .':count[{0} Want to buy more? for $:price?]';
echo StringReplace::replace($text, [
    'count' => 1,
    'price' => 9.99
]);
// You have 1 try left!
echo StringReplace::replace($text, [
    'count' => 0,
    'price' => 9.99
]);
// You have 0 tries left! Want to buy more? for $9.99?
$text = 'You have :count :count[tries|try|tries] left!'
    .':count[{0} Want to buy more? for $:price?| Keep playing!]';
echo StringReplace::replace($text, [
    'count' => 1,
    'price' => 9.99
]);
// You have 1 try left! Keep playing!

如果没有传递变量,它将保持原样(几乎)

$text = 'Hello, :name! There are :time :time[{_1}day|days] left until the new year';
echo self::stringReplace($text, [
    'time' => 11
]);
// Hello, :name! There are 11 days left until the new year
echo self::stringReplace($text);
// Hello, :name! There are :time :time[...] left until the new year

描述

此包包含一个特质文件,该文件允许您根据提供的数据在字符串中替换值。

示例

echo self::stringReplace('Привет, :name! До нового года осталось: :time', [
    'name' => 'Иван'
    'time' =>  '11 дней'
]);
// Привет, Иван! До нового года осталось: 11 дней

但这样并不特别有趣,对吧?让我们来否定日子吧!

$text = 'Привет, :name! До нового года осталось: :time :time[{_1}день|{_2-_4}дня|{11}дней|дней]';
echo self::stringReplace($text, [
    'name' => 'Иван'
    'time' =>  11
]);
// Привет, Иван! До нового года осталось: 11 дней

echo self::stringReplace($text, [
    'name' => 'Иван'
    'time' =>  2
]);
// Привет, Иван! До нового года осталось: 2 дня

echo self::stringReplace($text, [
    'name' => 'Иван'
    'time' =>  23
]);
// Привет, Иван! До нового года осталось: 23 дня

如果您决定“编程”文本...

$text = 'У вас осталось: :count :count[попыток|попытка|попыток]!'
    .':count[{0} Хотите приобрести еще? за $:price?]';
echo StringReplace::replace($text, [
    'count' =>  1,
    'price' => 9.99
]);
// У вас осталось: 1 попытка!

echo StringReplace::replace($text, [
    'count' =>  0,
    'price' => 9.99
]);
// У вас осталось: 0 попыток! Хотите приобрести еще? за $9.99?

$text = 'У вас осталось: :count :count[попыток|попытка|попыток]!'
    .':count[{0} Хотите приобрести еще? за $:price?| Играем дальше!]';
echo StringReplace::replace($text, [
    'count' =>  1,
    'price' => 9.99
]);
// У вас осталось: 1 попытка! Играем дальше!

如果未传递变量,则将保持原样(几乎)

$text = 'Привет, :name! До нового года осталось: :time :time[{_1}день|{_2-_4}дня|{11}дней|дней]';
echo self::stringReplace($text, [
    'time' =>  11
]);
// Привет, :name! До нового года осталось: 11 дней

echo self::stringReplace($text);
// Привет, :name! До нового года осталось: :time :time[...]