Guest User

Untitled

a guest
Jul 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. <?php
  2. $test =
  3. '<b>1.</b> This is a long text for testing plaintext to a userfriendly nl2p. The first example contains no tabs, no newlines but spaces. The second example contains single newlines only. The third example contains single and multiple newline. The forth example contains single and multiple newlines that are encapsulated by tabs.
  4.  
  5. <b>2.</b> This is a long text for testing plaintext to a userfriendly nl2p. The first example contains no tabs, no newlines but spaces.
  6. The second example contains single newlines only. The third example contains single and multiple newline.
  7. The forth example contains single and multiple newlines that are encapsulated by tabs.
  8.  
  9. <b>3.</b> This is a long text for testing plaintext to a userfriendly nl2p.
  10. The first example contains no tabs, no newlines but spaces.
  11.  
  12. The second example contains single newlines only.
  13.  
  14. The third example contains single and multiple newline.
  15. The forth example contains single and multiple newlines that are encapsulated by tabs.
  16.  
  17. <b>4.</b> This is a long text for testing plaintext to a userfriendly nl2p.
  18. The first example contains no tabs, no newlines but spaces.
  19.  
  20. The second example contains single newlines only.
  21.  
  22. The third example contains single and multiple newline.
  23.  
  24. The forth example contains single and multiple newlines that are encapsulated by tabs.
  25.  
  26. <b>5.</b> This additional example will have indents at its starting line, tabs and whitespaces:
  27.  
  28. this line is tabbed
  29. this line is tabbed
  30.  
  31. this line is spaced
  32. this line is spaced
  33.  
  34. this line is tabbed with space at the end
  35. this line is tabbed with space at the end
  36.  
  37. this line is spaced with space at the end
  38. this line is spaced with space at the end
  39.  
  40. this line is tabbed with tab at the end
  41. this line is tabbed with tab at the end
  42.  
  43. this line is spaced with tab at the end
  44. this line is spaced with tab at the end
  45.  
  46. <b>6.</b>
  47.  
  48. i hate this thing
  49. you know
  50.  
  51. grr how i hate it
  52.  
  53. double space this is after double space
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61. '; $text = $test;
  62.  
  63. $p_start = '<p>';
  64. $p_end = '</p>';
  65. $br = '<br />';
  66. if(!empty($text)) {
  67. $text = utf8_encode($text); // encode to utf8
  68. $text = trim($text);
  69. $text = preg_replace('/[\040|\011]{1,}/u',' ',$text); // 040 space, 011 tab, replace by single space
  70. $text = preg_replace('/[\n|\r][\040|\011]{1,}/u', "\n",$text); // remove trailing tabs and spaces
  71. $text = preg_replace('/[\040|\011]{1,}[\n|\r]/u', "\n",$text); // remove ending tabs and spaces
  72. $text = $p_start . preg_replace('#(\r?\n){2,}(\s+)?#u', $p_end . $p_start, $text) . $p_end; // replace double newlines with <p>
  73. $text = preg_replace('#\r?\n#u', $br, $text); // replace single newlines with <br>
  74. $text = preg_replace(array('#' . $p_end . '#u', '#' . $br . '#u'), array($p_end . "\n", $br . "\n"), $text); // add newlines to sourcecode for sourcode readabilityy
  75. }
  76.  
  77. ?>
  78. <pre>
  79. <?php echo $test?>
  80. </pre>
  81. <hr id="result" />
  82. <style>
  83. p { border: 1px solid red;}
  84. </style>
  85. <?php echo $text?>
Add Comment
Please, Sign In to add comment