3v4l.org

run code in 300+ PHP versions simultaneously
<?php // タグの条件 define("PAT_TAG", "(^|[\\s])([##]\\S+)"); // $str1 $str1 = 'タグは #1 #2 です'; echo "$str1\n"; var_dump( extract_tag_arr($str1) ); // -> `1`と`2`が得られる // str2 $str2 = 'タグは #1 #2 です'; echo "$str2\n"; var_dump( extract_tag_arr($str2) ); // -> `1 #2`が得られてしまう $str3 = 'タグは #1\20001#2 です'; echo "$str3\n"; var_dump( extract_tag_arr($str3) ); // -> `1\\20001#2`が得られてしまう // $str からタグ配列を得る function extract_tag_arr($str){ // タグ抽出 $tag_arr = []; preg_match_all('/'.PAT_TAG.'/u' , $str, $ms); foreach ( $ms[0] as $m ) { $tag_arr[] = preg_replace('/(\A[\p{Cc}\p{Cf}\p{Z}]++|[\p{Cc}\p{Cf}\p{Z}]++\z)(#|#)/u', '', $m); } return $tag_arr; }
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
タグは #1 #2 です array(2) { [0]=> string(1) "1" [1]=> string(1) "2" } タグは #1 #2 です array(2) { [0]=> string(1) "1" [1]=> string(1) "2" } タグは #1\20001#2 です array(1) { [0]=> string(9) "1\20001#2" }
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 タグは #1 #2 です array(2) { [0]=> string(1) "1" [1]=> string(1) "2" } タグは #1 #2 です array(2) { [0]=> string(1) "1" [1]=> string(1) "2" } タグは #1\20001#2 です array(1) { [0]=> string(9) "1\20001#2" }

preferences:
158.43 ms | 402 KiB | 150 Q