Created
September 13, 2020 10:33
-
-
Save ewwink/083d193d8949f0c7b4594ef40c3b7a0c to your computer and use it in GitHub Desktop.
Javascript Article/Text Spinner Support Nesting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* 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); |
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
C# version