PageRenderTime 73ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/string functions.php

https://gitlab.com/naznin/Naznin_151485_B33_session4
PHP | 153 lines | 133 code | 10 blank | 10 comment | 0 complexity | f47d85d090eabd9634a852b9c497b8d1 MD5 | raw file
  1. <?php
  2. //addslashes
  3. $str = "is your name j'ohn?";
  4. $str1='i will not go "there"';
  5. $str2="thank \you";
  6. echo addslashes($str);//outputs is your name j\'ohn?
  7. echo "</br>";
  8. echo addslashes($str1);//outputs i will not go \"there\"
  9. echo "</br>";
  10. echo addslashes($str2);//outputs thank \\you
  11. echo "</br>";
  12. ?>
  13. <?php
  14. // explode and join
  15. $class_list=" V,VI,VII,VIII,IX,X";
  16. $class_array = explode (",",$class_list);//spilts the input string and return as array
  17. print_r($class_array);
  18. echo "</br>";
  19. $class_string=join(",",$class_array);//join the input array and return as sring
  20. echo $class_string;
  21. echo "</br>";
  22. ?>
  23. <?php
  24. //htmlentities
  25. $convert = htmlentities ("<li><ahref='index.php'>we are learning PHP</a></li>",ENT_COMPAT);
  26. echo $convert;
  27. ?>
  28. <?php
  29. //join_string
  30. echo "</br>";
  31. $array_name= array('First name', 'Middle name', 'Last name');
  32. $join_string = implode("-",$array_name);
  33. echo $join_string ;
  34. ?>
  35. <?php
  36. //ltrim()
  37. echo "</br>";
  38. $string="\t\tpondit.com\t\t";
  39. $trim_string=ltrim($string);//removes \t\t or spaces
  40. var_dump($trim_string);//shows number of character in trimmed string and the trimmed string
  41. echo "</br>";
  42. $string1="good morning";
  43. $trim_string1=ltrim($string1,'g');//removes g from left side
  44. var_dump($trim_string1);//shows number of character in trimmed string and the trimmed string
  45. //rtrim
  46. echo "</br>";
  47. $trim_string=rtrim($string);//removes \t\t
  48. var_dump($trim_string);//shows number of character in trimmed string and the trimmed string
  49. echo "</br>";
  50. $trim_string1=rtrim($string1,'g');//removes gfrom right side
  51. var_dump($trim_string1);//shows number of character in trimmed string and the trimmed string
  52. echo "</br>";
  53. //trim
  54. echo "</br>";
  55. $trim_string=trim($string);//removes \t\t
  56. var_dump($trim_string);//shows number of character in trimmed string and the trimmed string
  57. echo "</br>";
  58. $trim_string1=trim($string1,'g');//removes g from both side
  59. var_dump($trim_string1);//shows number of character in trimmed string and the trimmed string
  60. echo "</br>";
  61. ?>
  62. <?php
  63. //nl2br
  64. echo nl2br("good \n morning");
  65. ?>
  66. <?php
  67. echo "</br>";
  68. echo str_repeat("PHP programming </br>",5);
  69. ?>
  70. <?php
  71. $string_name="what's your name ?";
  72. echo "</br>";
  73. print_r(str_split($string_name));
  74. echo "</br>";
  75. print_r(str_split($string_name,4));
  76. ?>
  77. <?php
  78. //strip_tags
  79. echo "</br>";
  80. $input_string="<b>welcome</b>";
  81. echo 'before strip:'.$input_string.'</br>';
  82. echo 'after strip:'.strip_tags($input_string);
  83. ?>
  84. <?php
  85. //strlen
  86. echo "</br>";
  87. $string_name='welcome to pondit.com';
  88. echo strlen($string_name);
  89. ?>
  90. <?php
  91. //strtolower
  92. $string1='GOOD MORNING';
  93. echo '</br>';
  94. echo strtolower($string1);
  95. ?>
  96. <?php
  97. //strtoupper
  98. $string1='good morning';
  99. echo '</br>';
  100. echo strtoupper($string1);
  101. ?>
  102. <?php
  103. //substr_compare
  104. echo "</br>";
  105. echo substr_compare("abcde","bc",1,2);
  106. ?>
  107. <?php
  108. //substr_count
  109. $text='this is a text';
  110. echo strlen($text);
  111. echo substr_count ($text,'is');
  112. echo substr_count ($text,'is',3);
  113. ?>
  114. <?php
  115. //substr_replace
  116. $string='abcdefgh';
  117. echo "</br>";
  118. echo substr_replace($string,'bob',2,4);
  119. ?>
  120. <?php
  121. str_pad
  122. echo "</br>";
  123. $str="Hello World";
  124. echo str_pad($str,20,".",STR_PAD_BOTH);//pads hello world with dots in both sides
  125. ?>
  126. <?php
  127. //ucfirst
  128. echo "</br>";
  129. $str="welcome";
  130. echo ucfirst($str);//converts w to W
  131. ?>
  132. <?php
  133. //str_replace
  134. echo str_replace("world","peter","Hello world !");//replaces world with peter
  135. ?>