Tag Archive for regex

Remove Non Numeric Characters

function  remove_non_numeric($string)
{
	return preg_replace('/D/', '', $string)
}

// OR

$result = ereg_replace( '[^0-9]+', '', $source );

source

php text to link

function replace_plain_text_link($plain_text) {
$url_html = preg_replace(
'/(?<!S)((http(s?)<a href="://)|(www.))+([w.1-9&=#?-~%;/]+)/'," >://)|(www.))+([w.1-9&=#?-~%;/]+)/',</a>
'<a href="http$3<a href="://$4$5">http$3://$4$5</a>'," >://$4$5">http$3://$4$5</a>',</a> $plain_text);
return ($url_html);
}
echo replace_plain_text_link("hi this is dummy text before
<a href="http://www.smjdesign.com" >http://www.smjdesign.com</a> hi this is dummy text after")

source

Password Strength Meter

PASSWORD STRENGTH METER (jquery)
/**
* Evaluate the strength of a user's password.
*
* Returns the estimated strength and the relevant output message.
*/
Drupal.evaluatePasswordStrength = function(value) {
  var strength = "", msg = "", translate = Drupal.settings.password;
  var hasLetters = value.match(/[a-zA-Z]+/);
  var hasNumbers = value.match(/[0-9]+/);
  var hasPunctuation = value.match(/[^a-zA-Z0-9]+/);
  var hasCasing = value.match(/[a-z]+.*[A-Z]+|[A-Z]+.*[a-z]+/);
  // Check if the password is blank.
  if (!value.length) {
    strength = "";
    msg = "";
  }
  // Check if length is less than 6 characters.
  else if (value.length < 6) {
    strength = "low";
    msg = translate.tooShort;
  }
  // Check if password is the same as the username (convert both to lowercase).
  else if (value.toLowerCase() == translate.username.toLowerCase()) {
    strength  = "low";
    msg = translate.sameAsUsername;
  }
  // Check if it contains letters, numbers, punctuation, and upper/lower case.
  else if (hasLetters && hasNumbers && hasPunctuation && hasCasing) {
    strength = "high";
  }
  // Password is not secure enough so construct the medium-strength message.
  else {
    // Extremely bad passwords still count as low.
    var count = (hasLetters ? 1 : 0) + (hasNumbers ? 1 : 0) + (hasPunctuation ? 1 : 0) + (hasCasing ? 1 : 0);
    strength = count > 1 ? "medium" : "low";
    msg = [];
    if (!hasLetters || !hasCasing) {
      msg.push(translate.addLetters);
    }
    if (!hasNumbers) {
      msg.push(translate.addNumbers);
    }
    if (!hasPunctuation) {
      msg.push(translate.addPunctuation);
    }
    msg = translate.needsMoreVariation +"<ul><li>"+ msg.join("</li><li>") +"</li></ul>";
  }
  return { strength: strength, message: msg };
};

source

Get Apache Version and other installed software versions as associative array

function getServerSoftware($key = 'SERVER_SOFTWARE')
{
	$regex = "/(?<software>w+)/(?<version>[0-9.a-z]*)/";
		
	if(isset($_SERVER[$key]) && preg_match_all($regex, $_SERVER[$key], $arr))
		return array_combine($arr['software'], $arr['version']);
	else
		return array();
}

//USAGE EXAMPLE

//get array
$serverInfo = getServerInfo();

//print apache version
echo $serverInfo['Apache'];

//print all information
foreach($serverInfo as $software=>$version) echo "{$software}: {$version}<br>
";

source

parseUri 1.2: Split URLs in JavaScript

// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:/?#]+):)?(?<a href="://((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:/?#]*)(?::(d*))?))?((((?:[^?#/]*/)*)([^?#]*))(?:?([^#]*))?(?:#(.*))?)/," >://((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:/?#]*)(?::(d*))?))?((((?:[^?#/]*/)*)([^?#]*))(?:?([^#]*))?(?:#(.*))?)/,</a>
		loose:  /^(?:(?![^:@]+:[^:@/]*@)([^:/?#.]+):)?(?<a href="://)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:/?#]*)(?::(d*))?)(((/(?:[^?#](?![^?#/]*.[^?#/.]+(?:[?#]|$)))*/?)?([^?#/]*))(?:?([^#]*))?(?:#(.*))?)/" >://)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:/?#]*)(?::(d*))?)(((/(?:[^?#](?![^?#/]*.[^?#/.]+(?:[?#]|$)))*/?)?([^?#/]*))(?:?([^#]*))?(?:#(.*))?)/</a>
	}
};

source

Anagram Validator

//regular expression
String patternStr = "(?=.*d.*d)(?=.*e)(?=.*b)(?=.*i)(?=.*t)(?=.*s)(?=.*c)(?=.*a)(?=.*r)^..........$";
boolean b;
Matcher matcher;
Pattern pattern;

System.out.println("Regex pattern = "+patternStr);
pattern = Pattern.compile(patternStr);//compiles the regex
		
//validates bad credit
System.out.println("Validating: bad credit");
matcher = pattern.matcher("bad credit");
b = matcher.matches();
if(b) System.out.println("bad credit: matched");
else System.out.println("bad credit: not matched");

//validates debit card
System.out.println("Validating: debit card");
matcher = pattern.matcher("debit card");
b = matcher.matches();
if(b) System.out.println("debit card: matched");
else System.out.println("debit card: not matched");

source

CHECK VALID EMAIL ADDRESS JQUERY

$(function(){
 
var email = $("input#email").val();
	if(isValidEmailAddress(email)) {
		$(email).after("hooray!");
	} else {
		$(email).addClass("yourerror").after("<label class='error'>Email is not valid!</label>").focus();
		return false;
	}
});
 
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[w-s]+")|([w-]+(?:.[w-]+)*)|("[w-s]+")([w-]+(?:.[w-]+)*))(@((?:[w-]+.)*w[w-]{0,66}).([a-z]{2,6}(?:.[a-z]{2})?)$)|(@[?((25[0-5].|2[0-4][0-9].|1[0-9]{2}.|[0-9]{1,2}.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2}).){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})]?$)/i);
return pattern.test(emailAddress);
}

source

AS3 Remove HTML Tags From Text

var myString:String = "<p><b>bold</b> <i>italic</i> <a href='#'>link</a> <br>linebreak</p>";
trace(myString)
var removeHtmlRegExp:RegExp = new RegExp("<[^<]+?>", "gi");
myString = myString.replace(removeHtmlRegExp, "");
trace(myString);

// OUTPUT
// <p><b>bold</b> <i>italic</i> <a href='#'>link</a> <br>linebreak</p>
// bold italic link linebreak

source

vim search and replace

# Replaces every occurrence of the word foo with bar in the whole file.
:%s/foo/bar/g

source

vim search and replace

:let i=1 | g/foo/s//=i."bar"/ | let i=i+1 

source