Skip to content

Instantly share code, notes, and snippets.

@joeyemery
Last active March 10, 2022 06:28
Show Gist options
  • Select an option

  • Save joeyemery/8437433 to your computer and use it in GitHub Desktop.

Select an option

Save joeyemery/8437433 to your computer and use it in GitHub Desktop.
Javascript str_replace function!
function str_replace(haystack, needle, replace) {
var haystack_arr = haystack.split('');
var needle_arr = needle.split('');
var replace_arr = replace.split('');
var replace_positions = new Array();
$.each(haystack_arr, function(key, value) {
if(value == needle_arr[0]) {
var correct_count = 1;
for(i = 1; i < needle_arr.length; i++) {
if(haystack_arr[key + i] == needle_arr[i]) {
correct_count++;
} else {
break;
}
}
if(correct_count == needle_arr.length) {
replace_positions.push(key);
}
}
});
$.each(replace_positions, function(key, value) {
value = value - ((needle_arr.length - 1) * key);
haystack_arr.splice((value + 1), (needle_arr.length - 1));
haystack_arr[value] = replace;
});
return haystack_arr.join('');
}

I haven't really bothered refactoring it (although I'm not sure it's possible to get it any smaller) and it does rely on jQuery (if only to make my life a tiny bit easier, it would still work without with minor changes!).

Right! There's 4 files.

  • str_replace.js: This is the function without any blank lines or comments.

  • str_replace_cleaner.js: Same as above with blank lines and comments.

  • str_replace_prototype: Same as above but it modifies the strings prototype so you can call the function the same way you'd call .replace()

  • str_replace.php The same challenge in PHP.

Pretty fucking nifty huh?

<?php
class String {
// Public vars.
public $string;
// Private vars.
private $needle_arr, $replace_arr, $haystack_arr, $replace_positions;
// Construct.
public function __construct($string) {
$this->string = $string;
}
// Replace function.
public function replace($needle, $replace) {
$this->haystack_arr = str_split($this->string);
$this->needle_arr = str_split($needle);
$this->replace_arr = str_split($replace);
$this->replace_positions = array();
foreach($this->haystack_arr as $key => $value) {
if($value == $this->needle_arr[0]) {
$correct_count = 1;
for($i = 1; $i < count($this->needle_arr); $i++) {
if($this->haystack_arr[$key + $i] == $this->needle_arr[$i]) {
$correct_count++;
} else {
break;
}
}
if($correct_count == count($this->needle_arr)) {
$replace_positions[] = $key;
}
}
}
foreach($replace_positions as $key => $value) {
$value = $value - ((count($this->needle_arr) -1) * $key);
array_splice($this->haystack_arr, $value, count($this->needle_arr), $replace);
}
$this->string = implode($this->haystack_arr, '');
return $this->string;
}
}
// Demo.
$string = new String('Hello. Hello. Hello.');
$string->replace('Hello', 'Goodbye');
echo $string->string;
?>
$(document).ready(function() {
str_replace('The rabbit is awesome, I mean who wouldnt love rabbits?', 'rabbit', 'fox');
});
function str_replace(haystack, needle, replace) {
// Split all the strings into arrays.
var haystack_arr = haystack.split('');
var needle_arr = needle.split('');
var replace_arr = replace.split('');
// Find the length of the needle array.
var replace_positions = new Array();
// Loop through each character of the haystack.
$.each(haystack_arr, function(key, value) {
// Check if this character matches the first of our needle.
if(value == needle_arr[0]) {
var correct_count = 1;
// Go through the next few letters to check if the full phrase is there.
for(i = 1; i < needle_arr.length; i++) {
if(haystack_arr[key + i] == needle_arr[i]) {
correct_count++;
} else {
break;
}
}
// Check if the correct count is the same as the needle length.
if(correct_count == needle_arr.length) {
replace_positions.push(key);
}
}
});
// Loop through the positions and replace all the letters!
$.each(replace_positions, function(key, value) {
// Calculate the offset based on previous replacements.
value = value - ((needle_arr.length - 1) * key);
haystack_arr.splice((value + 1), (needle_arr.length - 1));
haystack_arr[value] = replace;
});
return haystack_arr.join('');
}
$(document).ready(function() {
var test = 'The rabbit is awesome, I mean who wouldnt love rabbits?';
alert(test.joey_replace('rabbit', 'fox'));
});
String.prototype.joey_replace = function(needle, replace) {
// Split all the strings into arrays.
var haystack_arr = this.split('');
var needle_arr = needle.split('');
var replace_arr = replace.split('');
// Find the length of the needle array.
var replace_positions = new Array();
// Loop through each character of the haystack.
$.each(haystack_arr, function(key, value) {
// Check if this character matches the first of our needle.
if(value == needle_arr[0]) {
var correct_count = 1;
// Go through the next few letters to check if the full phrase is there.
for(i = 1; i < needle_arr.length; i++) {
if(haystack_arr[key + i] == needle_arr[i]) {
correct_count++;
} else {
break;
}
}
// Check if the correct count is the same as the needle length.
if(correct_count == needle_arr.length) {
replace_positions.push(key);
}
}
});
// Loop through the positions and replace all the letters!
$.each(replace_positions, function(key, value) {
// Calculate the offset based on previous replacements.
value = value - ((needle_arr.length - 1) * key);
haystack_arr.splice((value + 1), (needle_arr.length - 1));
haystack_arr[value] = replace;
});
return haystack_arr.join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment