PHPの標準関数をRubyで書く（文字列操作編）

## はじめに

PHPの標準関数をRubyで書いてみました。
簡単に書けそうなものだけパズル感覚で書いてます。

文字列操作編です。

引数と返り値の型はテーブルで記載しました。

## str_contains($haystack, $needle)
[PHP: str_contains - Manual](https://www.php.net/manual/ja/function.str-contains.php)

|||
---|---
haystack|String
needle|String
return|TrueClass &#x7C; FalseClass
```ruby:Ruby
haystack.include?(needle)
```

## str_ends_with($haystack, $needle)
[PHP: str_ends_with - Manual](https://www.php.net/manual/ja/function.str-ends-with.php)

|||
---|---
haystack|String
needle|String
return|TrueClass &#x7C; FalseClass
```ruby:Ruby
haystack.end_with?(needle)
```

## str_getcsv ($string, $separator, $enclosure, $escape)
[PHP: str_getcsv - Manual](https://www.php.net/manual/ja/function.str-getcsv.php)

|||
---|---
string|String
separator|String
enclosure|String
escape|標準CSVライブラリでは指定不可<br>(2回続けて入力してエスケープ)
return|Array&lt;String&gt;
```ruby:Ruby
require "csv"
CSV.parse_line(string, col_sep: separator, quote_char: enclosure)
```

## str_ireplace($search, $replace, $subject, &$count)
[PHP: str_ireplace - Manual](https://www.php.net/manual/ja/function.str-ireplace.php)

|||
---|---
search|String
replace|String
subject|String
count|Integer
return|String
```ruby:Ruby
count = 0
subject.gsub(Regexp.new(Regexp.escape(search), Regexp::IGNORECASE)) {count += 1; replace}
```

## str_pad($string, $length, $pad_string, $pad_type)
[PHP: str_pad - Manual](https://www.php.net/manual/ja/function.str-pad.php)

|||
---|---
string|String
length|Integer
pad_string|String
pad_type|STR_PAD_RIGHT
return|String
```ruby:Ruby
string.ljust(length, pad_string)
```

|||
---|---
string|String
length|Integer
pad_string|String
pad_type|STR_PAD_LEFT
return|String
```ruby:Ruby
string.rjust(length, pad_string)
```

|||
---|---
string|String
length|Integer
pad_string|String
pad_type|STR_PAD_BOTH
return|String
```ruby:Ruby
string.center(length, pad_string)
```

## str_repeat($string, $times)
[PHP: str_repeat - Manual](https://www.php.net/manual/ja/function.str-repeat.php)

|||
---|---
string|String
times|Integer
return|String
```ruby:Ruby
string * times
```

## str_replace($search, $replace, $subject, &$count)
[PHP: str_replace - Manual](https://www.php.net/manual/ja/function.str-replace.php)

|||
---|---
search|String
replace|String
subject|String
count|Integer
return|String
```ruby:Ruby
count = 0
subject.gsub(Regexp.new(Regexp.escape(search))) {count += 1; replace}
```

## str_rot13($string)
[PHP: str_rot13 - Manual](https://www.php.net/manual/ja/function.str-rot13.php)

|||
---|---
string|String
return|String
```ruby:Ruby
string.tr("a-zA-Z", "n-za-mN-ZA-M")
```

## str_shuffle($string)
[PHP: str_shuffle - Manual](https://www.php.net/manual/ja/function.str-shuffle.php)

|||
---|---
string|String
return|String
```ruby:Ruby
string.chars.shuffle.join
```

## str_split($string, $length)
[PHP: str_split - Manual](https://www.php.net/manual/ja/function.str-split.php)

|||
---|---
string|String
length|Integer ※1以上
return|String
```ruby:Ruby
string.each_char.each_slice(length).map(&:join)
```

## str_starts_with($haystack, $needle)
[PHP: str_starts_with - Manual](https://www.php.net/manual/ja/function.str-starts-with.php)

|||
---|---
haystack|String
needle|String
return|TrueClass &#x7C; FalseClass
```ruby:Ruby
haystack.start_with?(needle)
```

## str_word_count($string, $format, $characters)
[PHP: str_word_count - Manual](https://www.php.net/manual/ja/function.str-word-count.php)

|||
---|---
string|String
format|0
characters|String
return|Integer
```ruby:Ruby
string.match(/^[-']*(.*?)-*$/)[1].scan(/[-'a-zA-Z]+/).count
```

|||
---|---
string|String
format|1
characters|String
return|Array&lt;String&gt;
```ruby:Ruby
string.match(/^[-']*(.*?)-*$/)[1].scan(/[-'a-zA-Z]+/)
```

## strcasecmp($str1, $str2)
[PHP: strcasecmp - Manual](https://www.php.net/manual/ja/function.strcasecmp.php)

|||
---|---
str1|String
str2|String
return|Integer
```ruby:Ruby
str1.casecmp(str2)
```

※返却値は-1, 0, 1のいずれかなので注意

## strcmp($str1, $str2)
[PHP: strcmp - Manual](https://www.php.net/manual/ja/function.strcmp.php)

|||
---|---
str1|String
str2|String
return|Integer
```ruby:Ruby
str1 <=> str2
```

※返却値は-1, 0, 1のいずれかなので注意

## strcspn($string, $characters, $offset, $length)
[PHP: strcspn - Manual](https://www.php.net/manual/ja/function.strcspn.php)

|||
---|---
string|String
characters|String
offset|Integer
length|Integer ※0以上
return|Integer
```ruby:Ruby
string = string[offset, length]
string.chars.each_with_index.find { |c, _| characters.include?(c) }&.at(1) || string.length
```

## stripos($haystack, $needle, $offset)
[PHP: stripos - Manual](https://www.php.net/manual/ja/function.stripos.php)

|||
---|---
haystack|String
needle|String
offset|Integer
return|String &#x7C; FalseClass
```ruby:Ruby
haystack.downcase.index(needle.downcase, offset)
```

## stristr($haystack, $needle, $before_needle)
[PHP: stristr - Manual](https://www.php.net/manual/ja/function.stristr.php)

|||
---|---
haystack|String
needle|String
before_needle|FalseClass
return|String &#x7C; FalseClass
```ruby:Ruby
haystack.chars.each_with_index.find { |c, _| c.casecmp?(needle) }.then { |_, i| i ? haystack[i..] : false }
```

|||
---|---
haystack|String
needle|String
before_needle|TrueClass
return|String &#x7C; FalseClass
```ruby:Ruby
haystack.chars.each_with_index.find { |c, _| c.casecmp?(needle) }.then { |_, i| i ? haystack[0, i] : false }
```

## strlen($string)
[PHP: strlen - Manual](https://www.php.net/manual/ja/function.strlen.php)

|||
---|---
string|String
return|Integer
```ruby:Ruby
string.length
```

## strncasecmp($str1, $str2, $length)
[PHP: strncasecmp - Manual](https://www.php.net/manual/ja/function.strncasecmp.php)

|||
---|---
str1|String
str2|String
length|Integer
return|Integer
```ruby:Ruby
str1[0, length].casecmp(str2[0, length])
```

※返却値は-1, 0, 1のいずれかなので注意

## strncmp($str1, $str2, $length)
[PHP: strncmp - Manual](https://www.php.net/manual/ja/function.strncmp.php)

|||
---|---
str1|String
str2|String
length|Integer
return|Integer
```ruby:Ruby
str1[0, length] <=> str2[0, length]
```

※返却値は-1, 0, 1のいずれかなので注意

## strpbrk($string, $characters)
[PHP: strpbrk - Manual](https://www.php.net/manual/ja/function.strpbrk.php)

|||
---|---
string|String
characters|String
return|String &#x7C; FalseClass
```ruby:Ruby
string.chars.each_with_index.find { |c, _| characters.include?(c) }.then { |_, i| i ? string[i..] : false }
```

## strpos($haystack, $needle, $offset)
[PHP: strpos - Manual](https://www.php.net/manual/ja/function.strpos.php)

|||
---|---
haystack|String
needle|String
offset|Integer
return|String &#x7C; FalseClass
```ruby:Ruby
haystack.index(needle, offset)
```

## strrchr($haystack, $needle)
[PHP: strrchr - Manual](https://www.php.net/manual/ja/function.strrchr.php)

|||
---|---
haystack|String
needle|String
return|String &#x7C; FalseClass
```ruby:Ruby
haystack.chars.reverse.each_with_index.find { |c, _| c == needle.chr }.then { |_, i| i ? haystack[-i-1..] : false }
```

## strrev($string)
[PHP: strrev - Manual](https://www.php.net/manual/ja/function.strrev.php)

|||
---|---
string|String
return|String
```ruby:Ruby
string.reverse
```

## strripos($haystack, $needle, $offset)
[PHP: strripos - Manual](https://www.php.net/manual/ja/function.strripos.php)

|||
---|---
haystack|String
needle|String
offset|Integer ※0以上
return|String &#x7C; FalseClass
```ruby:Ruby
haystack[offset..].downcase.rindex(needle.downcase)&.+(offset) || false
```

|||
---|---
haystack|String
needle|String
offset|Integer ※0未満
return|String &#x7C; FalseClass
```ruby:Ruby
haystack.downcase.rindex(needle.downcase, offset) || false
```

## strrpos($haystack, $needle, $offset)
[PHP: strrpos - Manual](https://www.php.net/manual/ja/function.strrpos.php)

|||
---|---
haystack|String
needle|String
offset|Integer ※0以上
return|String &#x7C; FalseClass
```ruby:Ruby
haystack[offset..].rindex(needle)&.+(offset) || false
```

|||
---|---
haystack|String
needle|String
offset|Integer ※0未満
return|String &#x7C; FalseClass
```ruby:Ruby
haystack.rindex(needle, offset) || false
```

## strspn($string, $characters, $offset, $length)
[PHP: strspn - Manual](https://www.php.net/manual/ja/function.strspn.php)

|||
---|---
string|String
characters|String
offset|Integer
length|Integer ※0以上
return|Integer
```ruby:Ruby
string = string[offset, length]
string.chars.each_with_index.find { |c, _| !characters.include?(c) }&.at(1) || string.length
```

## strstr($haystack, $needle, $before_needle)
[PHP: strstr - Manual](https://www.php.net/manual/ja/function.strstr.php)

|||
---|---
haystack|String
needle|String
before_needle|FalseClass
return|String &#x7C; FalseClass
```ruby:Ruby
haystack.chars.each_with_index.find { |c, _| c == needle }.then { |_, i| i ? haystack[i..] : false }
```

|||
---|---
haystack|String
needle|String
before_needle|TrueClass
return|String &#x7C; FalseClass
```ruby:Ruby
haystack.chars.each_with_index.find { |c, _| c == needle }.then { |_, i| i ? haystack[0, i] : false }
```

## strtolower($string)
[PHP: strtolower - Manual](https://www.php.net/manual/ja/function.strtolower.php)

|||
---|---
string|String
return|String
```ruby:Ruby
string.downcase
```

## strtoupper($string)
[PHP: strtoupper - Manual](https://www.php.net/manual/ja/function.strtoupper.php)

|||
---|---
string|String
return|String
```ruby:Ruby
string.upcase
```

## strtr($string, $from, $to)
[PHP: strtr - Manual](https://www.php.net/manual/ja/function.strtr.php)

|||
---|---
string|String
from|String
to|String
return|String
```ruby:Ruby
limit = [from, to].map(&:length).min
string.tr(from[0, limit], to[0, limit])
```

## strtr($string, $replace_pairs)
[PHP: strtr - Manual](https://www.php.net/manual/ja/function.strtr.php)

|||
---|---
string|String
replace_pairs|Hash&lt;String, String&gt;
return|String
```ruby:Ruby
string.gsub(Regexp.new(replace_pairs.keys.map { |s| Regexp.escape(s) }.join('|'))) { |m| replace_pairs[m] }
```

## 参考文献

- Class array (Ruby 3.0.0 リファレンスマニュアル). (n.d.). docs.ruby-lang.org. Retrieved March 30, 2021, from https://docs.ruby-lang.org/ja/latest/class/Array.html

- Class CSV (Ruby 3.0.0 リファレンスマニュアル). (n.d.). docs.ruby-lang.org. Retrieved March 30, 2021, from https://docs.ruby-lang.org/ja/latest/class/CSV.html

- Class Regexp (Ruby 3.0.0 リファレンスマニュアル). (n.d.). docs.ruby-lang.org. Retrieved March 30, 2021, from https://docs.ruby-lang.org/ja/latest/class/Regexp.html

- Class string (Ruby 3.0.0 リファレンスマニュアル). (n.d.). docs.ruby-lang.org. Retrieved March 30, 2021, from https://docs.ruby-lang.org/ja/latest/class/String.html

- Module enumerable (Ruby 3.0.0 リファレンスマニュアル). (n.d.). docs.ruby-lang.org. Retrieved March 30, 2021, from https://docs.ruby-lang.org/ja/latest/class/Enumerable.html

- Str_contains. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-contains.php

- Str_ends_with. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-ends-with.php

- Str_getcsv. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-getcsv.php

- Str_ireplace. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-ireplace.php

- Str_pad. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-pad.php

- Str_repeat. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-repeat.php

- Str_replace. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-replace.php

- Str_rot13. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-rot13.php

- Str_shuffle. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-shuffle.php

- Str_split. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-split.php

- Str_starts_with. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-starts-with.php

- Str_word_count. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-word-count.php

- Strcasecmp. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strcasecmp.php

- Strcmp. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strcmp.php

- Strcspn. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strcspn.php

- Stripos. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.stripos.php

- Stristr. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.stristr.php

- Strlen. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strlen.php

- Strncasecmp. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strncasecmp.php

- Strncmp. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strncmp.php

- Strpbrk. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strpbrk.php

- Strpos. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strpos.php

- Strrchr. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strrchr.php

- Strrev. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strrev.php

- Strripos. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strripos.php

- Strrpos. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strrpos.php

- Strspn. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strspn.php

- Strstr. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strstr.php

- Strtolower. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strtolower.php

- Strtoupper. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strtoupper.php

- Strtr. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strtr.php