Skip to content

Instantly share code, notes, and snippets.

View TangChr's full-sized avatar

Christian Tang TangChr

View GitHub Profile
@TangChr
TangChr / ExtendForEach.cs
Created November 21, 2015 12:48
C#: Add condition to Extension Method .ForEach
using System;
using System.Collections.Generic;
using System.Linq;
namespace TangChr
{
public static class ExtendForEach
{
public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action, Func<T, bool> condition)
{
@TangChr
TangChr / WildcardMatching.cs
Last active November 21, 2015 12:57
C#: Match string with wildcard
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
namespace TangChr.Tools
{
public static class StringMatch
{
public static bool IsWildcardMatch(string search, string source)
{ // Wildcard: *
return Operators.LikeString(source, search, CompareMethod.Text);
@TangChr
TangChr / file_info.php
Last active November 21, 2015 12:58
PHP: Read comment-based file-info
<?php
function get_file_info( $file ) {
$fp = fopen( $file, 'r' );
$data = fread( $fp, 8192 ); // get first 8kb
fclose( $fp );
// Capture all the header within first comment block
if( !preg_match( '!.*?/\*(.*?)\*/!ms', $data, $matches ) )
return array();
@TangChr
TangChr / underline.htm
Last active November 21, 2015 12:59
CSS: Link with different color for underline
<!DOCTYPE html>
<html>
<head>
<title>Link Color</title>
</head>
<style>
body {
background: #2c3e50;
font-family: Arial, Verdana, sans-serif;
}
@TangChr
TangChr / parse_tweet.php
Last active November 21, 2015 13:03
PHP: Convert hastags, mentions & URLs to hyperlinks in a tweet.
<?php
require 'tweet_parser.php';
$userUrl = 'https://twitter.com/';
$tagUrl = 'https://twitter.com/hashtag/';
$my_tweet = "@TangChr: Here's a link: http://willitcompile.net, and here's a #hashtag.";
$formatted = parseTweet($my_tweet, $userUrl, $tagUrl);
$formatted2 = parseTweetPrefix($my_tweet, $userUrl, $tagUrl);
echo <<<HTML
@TangChr
TangChr / ArrayExtensions.cs
Created November 26, 2015 09:27
C#: Array Extension Methods
using System;
using System.Linq;
namespace TangChr
{
static class ArrayExtensions
{
public static TSource[] Remove<TSource>(this TSource[] source, TSource item)
{
var result = new TSource[source.Length - source.Count(s => s.Equals(item))];
@TangChr
TangChr / session-activator.php
Last active December 11, 2015 23:05
PHP: Enable sessions ($_SESSION) in WordPress plugins and themes
<?php
/*
Plugin Name: Session Activator
Description: Enable the use of sessions ($_SESSION) in plugins and themes.
Version: 1.0.0
Author: Christian Tang
Author URI: http://christiantang.dk
*/
add_action('init', 'session_activator_start', 1);
add_action('wp_logout', 'session_activator_end');
@TangChr
TangChr / archive.html
Last active June 6, 2016 08:42
Liquid/Jekyll: Monthly Archive
<ul class="post-archive">
{% for p in site.posts %}
<li>
{% assign fdate = p.date | date: '%b %Y' %}
{% if cur_date != fdate %}
{% assign cur_date = fdate %}
<h3>{{ p.date | date: '%B, %Y' }}</h3>
{% endif %}
<a href="{{ p.url }}">{{ p.title }}</a>
</li>
@TangChr
TangChr / .travis.yml
Last active March 2, 2017 15:57
Travis CI: Build and test Jekyll website using HTMLProofer
language: ruby
rvm: 2.3.3
branches:
only:
- master
script: bundle exec rake test
@TangChr
TangChr / clean-jekyll.cmd
Created June 18, 2016 14:19
Batch: Delete all Jekyll-related files and folders created during run time
rmdir _site /s /q
rmdir .sass-cache /s /q
del Gemfile.lock