Skip to content

Instantly share code, notes, and snippets.

Avatar

Niket Pathak niketpathak

View GitHub Profile
@niketpathak
niketpathak / generateExcerpt.php
Last active Dec 23, 2017
Generate an Excerpt from given content. Strips off html tags and preserves word boundaries
View generateExcerpt.php
<?php
echo generateExcerpt('Some long text is not really present here', 15); // Some long...
/**
* Generate an Excerpt for a given String
* @param string $content The content
* @param int $maxLength The maximum length of the desired excerpt
* @param string $more the string to use as more
* @return string
*/
@niketpathak
niketpathak / stopwords.js
Last active Dec 21, 2017
Remove Stop words using JS
View stopwords.js
/**
* Strip off Stopwords from a given string
* @param inputString The input string
* @param stopWords {optional} An array of stopwords
* @returns {string}
*/
function removeStopWords (inputString, stopWords) {
if (!!inputString) return '';
if (!!stopWords || (stopWords && stopWords.constructor !== Array)) {
stopWords = [ "a", "about", "above", "after", "again", "against", "all", "am", "an", "and", "any", "are", "as", "at", "be", "because", "been", "before", "being", "below", "between", "both", "but", "by", "could", "did", "do", "does", "doing", "down", "during", "each", "few", "for", "from", "further", "had", "has", "have", "having", "he", "he'd", "he'll", "he's", "her", "here", "here's", "hers", "herself", "him", "himself", "his", "how", "how's", "i", "i'd", "i'll", "i'm", "i've", "if", "in", "into", "is", "it", "it's", "its", "itself", "let's", "me", "more", "most", "my", "myself", "nor", "of", "on", "once", "only", "or", "other", "ought", "our", "ours", "ourselves", "out", "over", "ow
@niketpathak
niketpathak / slug.js
Last active Oct 12, 2018
Generate slug using Javascript
View slug.js
/**
* Generate a slug
* @param inputString The input String
* @returns {string}
*/
function slugify(inputString) {
return inputString.toString().toLowerCase().trim()
.replace(/&/g, '-and-') // Replace & with 'and'
.replace(/[\s\W-]+/g, '-') // Replace spaces, non-word characters and multiple-dashes with a single dash (-)
.replace(/(^-|-$)/g, '') // Remove dangling hypens in case slug begins or ends with a special character
@niketpathak
niketpathak / slug.php
Last active Jul 15, 2019
Generate Slug PHP (Safe/clean urls) Original resource: https://stackoverflow.com/a/2955878/4717533
View slug.php
<?php
echo slugify('Hello World///&?Welcome'); // hello-world-welcome
/**
* Generates a slug from the given string
* @param $input The input string
* @param string $replacement The character/string to use as the replacement value
* @return mixed|string
*/
You can’t perform that action at this time.