Tag Archive for text

Text Wrapping with Regular Expressions

def wrap_text(txt, col = 80)
txt.gsub(/(.{1,#{col}})( +|$)
?|(.{#{col}})/,
"13
")
end

source

Limited Text Function – PHP

A function to limit text
Of course you can also copy this code and use it freely in the scripts that you may write.

php:

<?php
function limit_text( $text, $limit )
{
// figure out the total length of the string
if( strlen($text)>$limit )
{
# cut the text
$text = substr( $text,0,$limit );
# lose any incomplete word at the end
$text = substr( $text,0,-(strlen(strrchr($text,' '))) );
}
// return the processed string
return $text;
}
?>

How do I use this function?
Now to use it, you'll just have to paste the function above, into your script or onto a 'includes' file (containing all your functions) if you prefer.

Let's assume you want to paste it with your script - so, this is what you could do:

php:

<?php

//  Filename: test.php
//  ==================

//  Our sample text
$long = 'Offers a discussion board, news portal, '
.'web-related articles, tutorials, scripts,'
.' tech blog, photo gallery, oekaki drawing,'
.' fun games, and more.';

//  we only want the text to be 15 characters long.
$short15 = limit_text( $long,15 );
//  perhaps we need one with just 6 characters.
$short06 = limit_text( $long,6 );

//  we output some text
echo "<p>$long</p>
";
echo "<p>$short15</p>
";
echo "<p>$short06</p>
";

//  ============================================
//  now we paste our LIMIT_TEXT() function below
//  ============================================

function limit_text( $text, $limit )
{
if( strlen($text)>$limit )
{
$text = substr( $text,0,$limit );
$text = substr( $text,0,-(strlen(strrchr($text,' '))) );
}
return $text;
}
?>

source

Add line numbers to a text file

$ nl <filename>

source

Parse an XML document with REXML and print some of the text nodes

require "rexml/document"
file = File.new( "MY_FILE.xml" )
doc = REXML::Document.new file
doc.elements.each("document/record/upload_list"){
|element| puts element.text if element.text
}

source

Numbers to Text (spanish)

'*********************************************************************
'
' This function allows you to convert a number to text
' It's in spanish but it's easy to convert to any other language.
'
'*********************************************************************

Dim xcen(9) 'centenas
Dim xdec(9) 'decenas
Dim xuni(9) 'unidades
Dim xexc(6) 'except
Dim ceros(9)

Function NumToText(pnumero)

Dim letras
Dim i
Dim c
Dim j
Dim xnumero
Dim xnum
Dim num
Dim digito
Dim numero_ent
Dim entero
Dim decimales
Dim temp

xcen(2) = "dosc"
xcen(3) = "tresc"
xcen(4) = "cuatroc"
xcen(5) = "quin"
xcen(6) = "seisc"
xcen(7) = "setec"
xcen(8) = "ochoc"
xcen(9) = "novec"
xdec(2) = "veinti"
xdec(3) = "trei"
xdec(4) = "cuare"
xdec(5) = "cincue"
xdec(6) = "sese"
xdec(7) = "sete"
xdec(8) = "oche"
xdec(9) = "nove"
xuni(1) = "uno"
xuni(2) = "dos"
xuni(3) = "tres"
xuni(4) = "cuatro"
xuni(5) = "cinco"
xuni(6) = "seis"
xuni(7) = "siete"
xuni(8) = "ocho"
xuni(9) = "nueve"
xexc(1) = "diez"
xexc(2) = "once"
xexc(3) = "doce"
xexc(4) = "trece"
xexc(5) = "catorce"
xexc(6) = "quince"
ceros(1) = "0"
ceros(2) = "00"
ceros(3) = "000"
ceros(4) = "0000"
ceros(5) = "00000"
ceros(6) = "000000"
ceros(7) = "0000000"
ceros(8) = "00000000"

c = 1
i = 1
j = 0

xnumero = cStr(pnumero)
If Cdbl(LTrim(RTrim(pnumero))) < 999999999.99 Then
numero_ent = Cdbl(Int(pnumero))
If Len(numero_ent) < 9 Then
numero_ent = ceros(9 - Len(numero_ent)) & numero_ent
End If
entero = Cdbl(Int(numero_ent))
decimales = (Cdbl(xnumero) - entero) * 100

Do While i < 8
temp = 0
num = Cdbl(Mid(numero_ent, i, 3))
xnum = Mid(numero_ent, i, 3)
digito = Cdbl(Mid(xnum, 1, 1))

'/* analizo el numero entero de a 3 */
If xnum = "000" Then
j = 0
Else
j = 1
If digito > 1 Then
letras = letras & xcen(digito) & "ientos "
End If
If Mid(xnum, 1, 1) = "1" And Mid(xnum, 2, 2) <> "00" Then
letras = letras & "ciento "
ElseIf Mid(xnum, 1, 1) = "1" Then
letras = letras & "cien "
End If

'/* analisis de las decenas */
digito = Cdbl(Mid(xnum, 2, 1))
If digito > 2 And Mid(xnum, 3, 1) = "0" Then
letras = letras & xdec(digito) & "nta "
temp = 1
End If

If digito > 2 And Mid(xnum, 3, 1) <> "0" Then
letras = letras & xdec(digito) & "nta y "

End If

If digito = 2 And Mid(xnum, 3, 1) = "0" Then
letras = letras & "veinte "
temp = 1
ElseIf digito = 2 And Mid(xnum, 3, 1) <> "0" Then
letras = letras & "veinti"

End If

If digito = 1 And Mid(xnum, 3, 1) >= "6" Then
letras = letras & "dieci"
ElseIf digito = 1 And Mid(xnum, 3, 1) < "6" Then
letras = letras & xexc(Cdbl(Mid(xnum, 3, 1) + 1))
temp = 1
End If
End If

if temp = 0 then
'/* analisis del ultimo digito */
digito = Cdbl(Mid(xnum, 3, 1))
If ((c = 1) Or (c = 2)) And xnum = "001" Then
letras = letras & "un"
Else
If ((c = 1) Or (c = 2)) And xnum >= "020" And Mid(xnum, 3, 1) = "1" Then
letras = letras & "un"
Else
If digito <> 0 Then
letras = letras & xuni(digito)
End If
End If
End If
end if

If j = 1 And i = 1 And xnum = "001" And c = 1 Then
letras = letras & " millon "
ElseIf j = 1 And i = 1 And xnum <> "001" And c = 1 Then
letras = letras & " millones "
ElseIf j = 1 And i = 4 And c = 2 Then
letras = letras & " mil "
End If
i = i + 3
c = c + 1
Loop
If letras = "" Then
letras = "cero "
End If
If decimales <> 0 Then
decimales = Round(decimales)

letras = letras & " con " & CStr(decimales) & "/100"
End If

End If

NumToText = letras
End Function

source

Replace text with image

#ID_OF_ELEMENT {
padding: HEIGHT_OF_IMAGEpx 0 0 0;
overflow: hidden;
background-image: url("hello_world.gif");
background-repeat: no-repeat;

height: 0px !important;
height /**/:HEIGHT_OF_IMAGEpx;
}

source

Scrollbar for dynamic text

(Source code is in FLA)

source

AS2 createEmptyMovieClip MC配置サンプル

var container:MovieClip = this.createEmptyMovieClip("container", this.getNextHighestDepth());
var label:TextField = container.createTextField("label", 1, 0, 0, 150, 20);
label.text = "Hello World";

source

PHP 文字列を区切り文字で分割し、配列に変換する

// "abcd" と "fg" に分割される
$elements = split( "e", "abcdefg" );

//分割された文字列を確認
foreach( $elements as $element ){
print $element . "n";
}

// 正規表現を使用
//"abc" と "g" に分割される
$elements = split( ".e.", "abcdefg" );

//分割された文字列を確認
foreach( $elements as $element ){
print $element . "n";
}

source

AS2 MdN テキストフィールドをダイナミックに生成

/* ここから */
/*
MdN P039
テキストフィールドをダイナミックに生成
*/
//座標とサイズは後で再定義するので、適当な値を入れておく<1>
this.createTextField("myTextField",1,0,0,0,0);

//真中揃え<2>
myTextField.autoSize = "center";

//座標をステージの中央に合わせる
myTextField._x = Stage.width/2 - myTextField._width/2;
myTextField._y = Stage.height/2 - myTextField._height/2;

//<3>
var format:TextFormat = new TextFormat();
format.size = 20;
myTextField.setNewTextFormat(format);
myTextField.border = true;

//<4>
setInterval(shiftText,1000);

var tangoArray:Array = new Array(	"うみ","かえる","すずむし","すいかわり","プールびより","そうめんながし");
function shiftText():Void{
var randomNum = Math.round(Math.random()*5);
var tango = tangoArray[randomNum];

//文字列を代入
myTextField.text = tango;
}

source