Skip to content

Instantly share code, notes, and snippets.

@lenivene
Last active August 22, 2020 03:15
Show Gist options
  • Save lenivene/8571963f1cfee2dce325cd025b013eb4 to your computer and use it in GitHub Desktop.
Save lenivene/8571963f1cfee2dce325cd025b013eb4 to your computer and use it in GitHub Desktop.
TEXT LINE TO JSON PHP FUNCTION

TEXT LINE TO JSON

Powered By | Lenivene

$text = "hello world
Hi Baby
Nice";

$text_line_json = text_line_json_encode( $text );

echo "<pre>";
print_r( $text_line_json );
echo "</pre>";
<?php
function text_line_json_encode( $value, $options = 0, $depth = 512 ){
$to_json = $value;
if( ! is_array( $value ) || ! is_object( $value ) ){
$to_json = preg_split("/\r\n|\n|\r/", (string) $value );
}
return json_encode( $to_json, $options, $depth );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment