You can use explode()
or preg_split()
, like this:
$i = 0;
foreach( $namesArray as $key => $name ) {
$arr = explode( ' -', $name, 2 );
//$arr = preg_split( '/\s+\-/', $name, 2 );
$name2 = isset( $arr[0] ) ? trim( $arr[0] ) : '';
$role_name = isset( $arr[1] ) ? trim( $arr[1] ) : '';
if ( $name2 || $role_name ) : $i++; ?>
<tr>
<th scope="row"><?php echo $i; ?></th>
<td><?php echo $name2; ?></td>
<td><?php echo $role_name; ?></td>
</tr>
<?php endif;
}
Also, you need to wrap the \n
in double-quotes:
$namesArray = explode( "\n", $names); // not '\n'
Otherwise, the \n
will not be evaluated as "new line".