3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function inject_hyphens($word, $positions) { $pad = 0; foreach ($positions as $position) { $word = substr($word, 0, $position + $pad + 1) . '-' . substr($word, $position + $pad + 1, strlen($word)); $pad++; } return $word; } $input = array('syl-labification', 'sylla-bification', 'syllabi-fication', 'syllabifi-cation', 'syllabifica-tion', 'syllabification' ); //wanted $output = 'syl-la-bi-fi-ca-tion'; $positions = array(); foreach($input as $word) { preg_match_all('/-/', $word, $matches, PREG_OFFSET_CAPTURE); foreach($matches[0] as $key => $value) { $positions[] = $value[1]; } } $positions = array_unique($positions); var_dump(inject_hyphens('syllabification', $positions));
Output for 4.3.3 - 7.1.0
string(20) "syll-ab-if-ic-at-ion"
Output for 4.3.0 - 4.3.2
Warning: Wrong value for parameter 4 in call to preg_match_all() in /in/WRkvP on line 28 Warning: Invalid argument supplied for foreach() in /in/WRkvP on line 29 Warning: Wrong value for parameter 4 in call to preg_match_all() in /in/WRkvP on line 28 Warning: Invalid argument supplied for foreach() in /in/WRkvP on line 29 Warning: Wrong value for parameter 4 in call to preg_match_all() in /in/WRkvP on line 28 Warning: Invalid argument supplied for foreach() in /in/WRkvP on line 29 Warning: Wrong value for parameter 4 in call to preg_match_all() in /in/WRkvP on line 28 Warning: Invalid argument supplied for foreach() in /in/WRkvP on line 29 Warning: Wrong value for parameter 4 in call to preg_match_all() in /in/WRkvP on line 28 Warning: Invalid argument supplied for foreach() in /in/WRkvP on line 29 Warning: Wrong value for parameter 4 in call to preg_match_all() in /in/WRkvP on line 28 Warning: Invalid argument supplied for foreach() in /in/WRkvP on line 29 string(15) "syllabification"