PHP Converting Line Breaks – String Function

<?php
function line_break_set($subject, $type) {    switch ($type) {
        case 'mac':
            $ending = '\r';
            break;
        case 'pc':
            $ending = '\r\n';
            break;
        default:
            $ending = '\n';
    }    return preg_replace('/\r\n|\n\r|\n|\r/', $ending, $subject);
}$str = "Multiple\rtypes of\n\rline breaks\r\nhave been placed
within this string\n\nSee?";$mac = line_break_set($str, 'mac');
$unix = line_break_set($str, 'unix');
$pc = line_break_set($str, 'pc');echo '<pre>mac = ', addcslashes($mac, "\n\r"), "\npc = ",
    addcslashes($pc, "\n\r"), "\nunix = ",
    addcslashes($unix, "\n\r"), '

‘;
?>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.