i am working on Laravel Zero and created new command that will display the user input to show in uppercase and lowercase.
is there a way to also display the output alternate upper and lower case?
here is the command:
class UppercaseCommand extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'converts';
/**
* The description of the command.
*
* @var string
*/
protected $description = 'Converts the string to uppercase, lowercase, and alternate';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$a = readline('Enter a string: ');
echo "Output in uppercase: " , strtoupper($a). PHP_EOL;
echo "Output in lowercase: " , strtolower($a);
}
/**
* Define the command's schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
public function schedule(Schedule $schedule): void
{
// $schedule->command(static::class)->everyMinute();
}
}
so how can i add new line to show the input for example like this: "hElLo wOrLd"?
you can use this library in laravel here the example to understand this
it is easy way to use it
$lower=Illuminate\Support\Str::lower($value);
$upper=Illuminate\Support\Str::upper($value);
The Answer:
$chars = str_split($a);
foreach($chars as $char){
if ($UpperLowerSwitch){
echo strtolower($char);
$UpperLowerSwitch = false;
}else {
echo strtoupper($char);
$UpperLowerSwitch = true;
}
}
Output(if the user insert "hello world"):
hElLo wOrLd
If you want a shorter version, you can use the following code:
$str = "stack overflow";
$arr = str_split($str);
$result = join(array_map(function($char, $i) {
return $i % 2 ? strtoupper($char) : strtolower($char);
}, $arr, array_keys($arr)));
// Result: "sTaCk oVeRfLoW"
This uses the index of the character array ( i % 2
are the uneven indexes) to change the capitalization.
Wrap it into a nice function named unreadableCapitalization($str)
and be done with it:)
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.