Tag Archive for text

Case insensitive jQuery :contains selector

// For jQuery 1.2

jQuery.extend(
    jQuery.expr[':'], { 
        Contains : "jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0" 
});


// For jQuery 1.3 (thanks @user95227) and later you need

jQuery.expr[':'].Contains = function(a,i,m){
     return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
};

source

Shorten text class

public class CropText
{
    public CropText()
    {
    }

    public string ShortenText(string input, int MaxLenght, bool DoDots)
    {
        string result = input;
        int InputLength = input.Length;

        string lastChar = result.Substring(result.Length - 1);

        if (MaxLenght < InputLength)
        {
            result = input.Substring(0, MaxLenght);

            if (lastChar == " ")
            {
                result = result.Substring(0, result.Length - 1);
            }

            if (DoDots)
            {
                result += "...";
            }
        }
        return result;
    }
}

source

AS3 Load External Text File

package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class Main extends MovieClip
	{
		private var _txtFileToLoad:String = "myTextFile.txt";
		private var _txtLoader:URLLoader;
		private var _loadedTxt:String;
		

		public function Main():void
		{
			loadExternalTxtFile();
		}
		
		private function loadExternalTxtFile():void
		{
			_txtLoader = new URLLoader();
			_txtLoader.addEventListener(Event.COMPLETE, onTxtLoader_COMPLETE);
			_txtLoader.addEventListener(IOErrorEvent.IO_ERROR, onTxtLoader_IO_ERROR);
			_txtLoader.load(new URLRequest(_txtFileToLoad));
		}
		
		private function onTxtLoader_COMPLETE(event:Event):void {
			_txtLoader.removeEventListener(Event.COMPLETE, onTxtLoader_COMPLETE);
			_txtLoader.removeEventListener(IOErrorEvent.IO_ERROR, onTxtLoader_IO_ERROR);
			_loadedTxt = String(event.target.data);
			trace(_loadedTxt);
		}
		
		private function onTxtLoader_IO_ERROR(event:Event):void {
			_txtLoader.removeEventListener(Event.COMPLETE, onTxtLoader_COMPLETE);
			_txtLoader.removeEventListener(IOErrorEvent.IO_ERROR, onTxtLoader_IO_ERROR);
			trace(event.target.text);
		}

	}
}

source

Format a string with an array of arguments

/**
 * Format a string with an array of arguments.
 * 
 * @param msg A string.
 * @return A formatted string
 */
function format(msg) {
    if (arguments && arguments.length > 1) {
        for (var i = 1; i < arguments.length; i++) {
            msg = msg.replace('{'+(i-1)+'}', arguments[i]);
        }
    }
    return msg;
}

source

Changing text case utility

#!/usr/bin/perl -w
use strict;
use 5.010;

my ($type, $string);

print "What type of case? (upper/lower):
";
while (<STDIN>) {
	chomp;
	if (/^(upper|lower)$/i) {
		$type = $_;
		last;
	} else {
		print "Invalid type please type upper or lower:
";
	}
}

print "Please enter your text:
";
chomp($string = <STDIN>);
given ($type) {
	when ("upper") { print "U$string" }
	when ("lower") { print "L$string" }
}

print "
";

source

Wrapping Long URLs and Text Content with CSS

pre {
	white-space: pre;           /* CSS 2.0 */
	white-space: pre-wrap;      /* CSS 2.1 */
	white-space: pre-line;      /* CSS 3.0 */
	white-space: -pre-wrap;     /* Opera 4-6 */
	white-space: -o-pre-wrap;   /* Opera 7 */
	white-space: -moz-pre-wrap; /* Mozilla */
	white-space: -hp-pre-wrap;  /* HP Printers */
	word-wrap: break-word;      /* IE 5  */
	}

source

Corta los primeros n caracteres de un texto, pero busca el espacio para no cortar una palabra

if (strlen($texto) > 500) {
	$texto = substr($texto,0,strpos($texto," ",500))." ...";
}

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

CSS Ellipsis, text-overflow

li {
	white-space: nowrap;
	width: 100%;
	overflow: hidden;
        text-overflow: ellipsis;
	-o-text-overflow: ellipsis;
        -ms-text-overflow: ellipsis;
}

source

AS3 Loading and Using an External CSS File

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	public class CSSFormattingExample extends Sprite {
		var loader:URLLoader;
		var field:TextField;
		var exampleText:String = "<h1>This is a headline</h1>" +
		"<p>This is a line of text. <span class='bluetext'>" +
		"This line of text is colored blue.</span></p>";

		public function CSSFormattingExample():void {
			field = new TextField();
			field.width=300;
			field.autoSize=TextFieldAutoSize.LEFT;
			field.wordWrap=true;
			addChild(field);

			var req:URLRequest=new URLRequest("example.css");

			loader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onCSSFileLoaded);
			loader.load(req);
		}

		public function onCSSFileLoaded(event:Event):void {
			var sheet:StyleSheet = new StyleSheet();
			sheet.parseCSS(loader.data);
			field.styleSheet=sheet;
			field.htmlText=exampleText;
		}
	}
}


// THE 'example.css' FILE SHOULD LOOK LIKE THIS ...
//p {
//	font-family: Times New Roman, Times, _serif;
//	font-size: 14;
//}
//
//h1 {
//	font-family: Arial, Helvetica, _sans;
//	font-size: 20;
//	font-weight: bold;
//}
//
//.bluetext {
//	color: #0000CC;
//}

source