Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $str = str_replace("ll", "", "good golly miss molly!", $count);
- echo $count; // 2
- function str_replace_once($search, $replace, $text){
- $pos = strpos($text, $search);
- return $pos!==false ? substr_replace($text, $replace, $pos, strlen($search)) : $text;
- }
- $str = 'Helo World!';
- $str = str_replace_once('l', 'll', $str);
- // $str = 'Hello World!';
- <?php
- function replacment($search, $replace, $text, $c){
- if($c > substr_count($text, $search)){
- return false;
- }
- else{
- $arr = explode($search, $text);
- $result = '';
- $k = 1;
- foreach($arr as $value){
- $k == $c ? $result .= $value.$replace : $result .= $value.$search;
- $k++;
- }
- $pos = strripos($result,$search);
- $result = substr_replace($result,'', $pos, $pos + 3);
- return $result;
- };
- }
- $st = 'Helo World and cool gold!';
- $str = replacment('l', 'bb', $st,'2');
- echo $str;
- // $str = 'Helo Worbbd and cool gold!';
- ?>
- $s='hello world, hello';
- $s=implode('i',explode('ello',$s,2));
- // hi world, hello
- $s='hello torld, test';
- if($p=strpos($s,'t')){
- $s[$p]='w';
- }
- // hello world, test
Add Comment
Please, Sign In to add comment