Skip to content

Instantly share code, notes, and snippets.

@ewwink
Created September 13, 2020 10:33
Show Gist options
  • Save ewwink/083d193d8949f0c7b4594ef40c3b7a0c to your computer and use it in GitHub Desktop.
Save ewwink/083d193d8949f0c7b4594ef40c3b7a0c to your computer and use it in GitHub Desktop.
Javascript Article/Text Spinner Support Nesting
/*
*
* credit https://www.blackhatworld.com/seo/get-article-spinner-in-html-support-nesting.422056/
*
*/
function GetSpinContent(text) {
var result = text,
match,
matches,
array = [],
reg = new RegExp(/{([^{}]*)\}/i);
while ((matches = reg.exec(result))) {
array = matches[1].split('|');
result = result.replace(matches[0], array[Math.floor(Math.random() * array.length)]);
}
reg = new RegExp(/\{\{([\s\S]*?)\}\}/i);
while ((match = reg.exec(result))) {
array = match[1].split('||');
result = result.replace(match[0], array[Math.floor(Math.random() * array.length)]);
}
return result;
}
var spun = GetSpinContent('{Hello|Howdy|{Hola|Hi}} to you, {{Smith|Williams}|{britney|christina}}!')
console.log(spun);
@ewwink
Copy link
Author

ewwink commented Jul 17, 2022

C# version

using System;
using System.Text.RegularExpressions;

public static string GetSpinContent(string text)
{
	string result = text;
	string[] array;
	MatchCollection match;
	MatchCollection matches;
	var reg = new Regex(@"{([^{}]*)\}");
	Random rnd = new Random();
	while ((matches = reg.Matches(result)).Count > 0)
	{
		array = matches[0].Groups[1].Value.Split('|');
		var randIndex = Math.Floor(rnd.NextDouble() * array.Length);
		var firstRegex = new Regex(Regex.Escape(matches[0].Value));
		result = firstRegex.Replace(result, array[Convert.ToInt32(randIndex)], 1);
	}

	rnd = new Random();
	reg = new Regex(@"\{\{([\s\S]*?)\}\}");
	while ((match = reg.Matches(result)).Count > 0)
	{
		array = match[0].Groups[1].Value.Split(new char[]{'|', '|'});
		var randIndex = Math.Floor(rnd.NextDouble() * array.Length);
		var firstRegex = new Regex(Regex.Escape(matches[0].Value));
		result = firstRegex.Replace(result, array[Convert.ToInt32(randIndex)]);
	}

	return result;
}

@ewwink
Copy link
Author

ewwink commented Jul 25, 2022

Python Version

import re
import random

def GetSpinContent(text):
    result = text
    array = []
    reg = r"{([^{}]*)\}"
    match = None
    matches = re.search(reg, result)

    while matches:
        array = matches.group(1).split('|')
        randIndex = random.randint(0, len(array) - 1)
        result = result.replace(matches.group(0), array[randIndex], 1)
        matches = re.search(reg, result)

    reg = r"\{\{([\s\S]*?)\}\}"
    match = re.search(reg, result)
    while match:
        array = matches.group(1).split('||')
        randIndex = random.randint(0, len(array) - 1)
        result = result.replace(matches.group(0), array[randIndex], 1)

    return result

print(GetSpinContent("{Hai|Hello|Howdy|{Hola|Hi}} to you, {{Smith|Williams}|{britney|christina}}!"))
print(GetSpinContent("{b|c|d|f|g|h|n|l|m|p|r|t}{a|i|u|e|o}{b|c|d|f|g|h|n|l|m|p|r|t}{a|i|u|e|o}"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment