Advertisement
em89

Strings in PHP

Jul 8th, 2014
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. //1.Removes Names.
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Print Array</title>
  6. </head>
  7. <body>
  8. <form method="post">
  9. <label for="first">First list:</label>
  10. <input type="text" name="text_1" id="first">
  11. <br>
  12. <label for="second">Second list:</label>
  13. <input type="text" name="text_2" id="second">
  14. <br>
  15. <input type="submit" value="Go">
  16. </form>
  17. <?php
  18. $str_1 = $_POST['text_1'];
  19. $str_2 = $_POST['text_2'];
  20. $array_1 = explode(" ", $str_1);
  21. $array_2 = explode(" ", $str_2);
  22. $together = array_diff($array_1, $array_2);
  23. echo implode(" ", $together);
  24.  
  25. ?>
  26. </body>
  27. </html>
  28.  
  29.  
  30.  
  31. //2.Count of Letters.
  32. <!DOCTYPE html>
  33. <html>
  34. <head>
  35. <title>Count of Letters</title>
  36. </head>
  37. <body>
  38. <form method="post">
  39. <label for="letters">Letters:</label>
  40. <input type="text" name="str" id="letters">
  41. <br>
  42. <input type="submit" value="Go">
  43. </form>
  44. <?php
  45. $str = $_POST['str'];
  46. $array = explode(" ", $str);
  47. sort($array);
  48. echo '<pre>' . print_r(array_count_values($array), true) . '</pre>';
  49. ?>
  50. </body>
  51. </html>
  52.  
  53.  
  54.  
  55. //3.Find All Matches.
  56. <!DOCTYPE html>
  57. <html>
  58. <head>
  59. <title>Find All Matches</title>
  60. </head>
  61. <body>
  62. <form method="post">
  63. <label for="text">Enter text:</label>
  64. <br>
  65. <textarea id="text" name="text" rows="10"></textarea>
  66. <br>
  67. <input type="submit" value="Go">
  68. </form>
  69. <?php
  70. $str = $_POST['text'];
  71. $str = strtolower($str);
  72. echo substr_count($str, 'em');
  73. ?>
  74. </body>
  75. </html>
  76.  
  77.  
  78.  
  79. //4.Extract all Valid URLs.
  80. <!DOCTYPE html>
  81. <html>
  82. <head>
  83. <title>Extract all valid URLs</title>
  84. </head>
  85. <body>
  86. <form method="POST">
  87. <label for="text">Enter text:</label>
  88. <input type="text" name="str" id="text">
  89. <input type="submit" value="Go">
  90. </form>
  91. <?php
  92. $str = $_POST['str'];
  93. $reg = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
  94. preg_match_all($reg, $str, $url);
  95. echo '<pre>' . implode("\n", $url[0]) . '</pre>';
  96. ?>
  97. </body>
  98. </html>
  99.  
  100.  
  101.  
  102. //6.Combine List of Letters.
  103. <!DOCTYPE html>
  104. <html>
  105. <head>
  106. <title>Combine List of Letters</title>
  107. </head>
  108. <body>
  109. <form method="post">
  110. <label for="first">First list:</label>
  111. <input type="text" name="text_1" id="first">
  112. <br>
  113. <label for="second">Second list:</label>
  114. <input type="text" name="text_2" id="second">
  115. <br>
  116. <input type="submit" value="Go">
  117. </form>
  118. <?php
  119. $str_1 = $_POST['text_1'];
  120. $str_2 = $_POST['text_2'];
  121. $array_1 = explode(" ", $str_1);
  122. $array_2 = explode(" ", $str_2);
  123. $unique = array_diff($array_2, $array_1);
  124. $together = array_merge($array_1, $unique);
  125. echo implode(" ", $together);
  126. ?>
  127. </body>
  128. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement