Instantly share code, notes, and snippets.

Embed
What would you like to do?
Nurble PHP vs C#
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
class Nurblizador
{
private static string[] _nouns = File.ReadAllText("nouns.txt").Split("\n");
static string Nurble(string text)
{
var pattern = Regex.Split(text.ToLower(), "[^a-z]")
.Where(w => !string.IsNullOrWhiteSpace(w))
.Distinct()
.Join(_nouns, n => n, n => n, (w, n) => $@"\b{n}\b")
.Aggregate((a,b) => $"{a}|{b}");
var replacement = @"<span class=""nurble"">nurble</span>";
return Regex.Replace(text.ToUpper(), pattern, replacement, RegexOptions.IgnoreCase).Replace("\n", "<br>");
}
}
<?php
function nurble($text)
{
$nouns = file('nouns.txt', FILE_IGNORE_NEW_LINES);
$isMatch = (bool) preg_match_all('/\b[a-zA-Z]+\b/', $text, $matches);
if ($isMatch === true) {
$words = array_unique(
array_filter($matches[0], function ($word) use ($nouns) {
return in_array($word, $nouns) === false;
})
);
$pattern = sprintf('/(\b)%s(\b)/', implode('(\b)|(\b)', $words));
$replacement = '\1<span class="nurble">nurble</span>\2';
$text = preg_replace($pattern, $replacement, $text);
}
return str_replace("\n", '<br>', $text);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment