3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function fixAddress($address) { $address = trim($address); $addr_words = str_word_count($address, 1, "/',.-$#@&*!+=0123456789"); $addr_line1 = ''; foreach ($addr_words as $word) { if(strlen($addr_line1.' '.$word) <= 30) { $addr_line1 .= ' '.$word; array_shift($addr_words); } else { break; } } $addr_line2 = ''; if(!empty($addr_words)) { $addr_line2 = substr(implode(' ', $addr_words), 0, 30); } return array('addr_line1'=>trim($addr_line1), 'addr_line2'=>trim($addr_line2)); } $t = fixAddress('201 12th Street South, South, $word'); print_r($t); exit;
based on Esap1
Output for 5.6.0 - 7.2.6
Array ( [addr_line1] => 201 12th Street South, South, [addr_line2] => $word )