Guest User

Untitled

a guest
May 27th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. $str = str_replace("ll", "", "good golly miss molly!", $count);
  2. echo $count; // 2
  3.  
  4. function str_replace_once($search, $replace, $text){
  5. $pos = strpos($text, $search);
  6. return $pos!==false ? substr_replace($text, $replace, $pos, strlen($search)) : $text;
  7. }
  8.  
  9. $str = 'Helo World!';
  10. $str = str_replace_once('l', 'll', $str);
  11. // $str = 'Hello World!';
  12.  
  13. <?php
  14. function replacment($search, $replace, $text, $c){
  15. if($c > substr_count($text, $search)){
  16. return false;
  17. }
  18. else{
  19. $arr = explode($search, $text);
  20. $result = '';
  21. $k = 1;
  22. foreach($arr as $value){
  23. $k == $c ? $result .= $value.$replace : $result .= $value.$search;
  24. $k++;
  25. }
  26. $pos = strripos($result,$search);
  27. $result = substr_replace($result,'', $pos, $pos + 3);
  28. return $result;
  29. };
  30. }
  31. $st = 'Helo World and cool gold!';
  32. $str = replacment('l', 'bb', $st,'2');
  33. echo $str;
  34. // $str = 'Helo Worbbd and cool gold!';
  35. ?>
  36.  
  37. $s='hello world, hello';
  38. $s=implode('i',explode('ello',$s,2));
  39. // hi world, hello
  40.  
  41. $s='hello torld, test';
  42. if($p=strpos($s,'t')){
  43. $s[$p]='w';
  44. }
  45. // hello world, test
Add Comment
Please, Sign In to add comment