alter_entry |
append_arg |
quicksort |
rehtml |
remove_arg |
replace_text |
split_left |
split_right |
split |
split_mult |
split2 |
striphtmlnew |
strstralter_entry(string original) : string "Alter text by find/replace"
mageboltrat's
industrial nipponfunction alter_entry(string original) : string "Alter text by find/replace"
#######################################################
# from lj:mageboltrat's industrial nippon (id=533012) #
# using lj:mageboltrat's replace_text function #
#######################################################
{
return $original;
}to be overridden like:
function alter_entry(string original) : string {
var string find = "";
var string replace = "";
$find = "
http://stat.livejournal.com/img/userinfo.gif";
$replace = "
http://pics.livejournal.com/img/fb-userinfo.gif";
$original = replace_text($original, $find, $replace);
return $original;
}and to be called like:
function Page::print_entry(Entry e){
if(not $e.text_must_print_trusted) {
print alter_entry($e.text);
} else {
$e->print_text();
}
}and will change all occurences of "
http://stat.livejournal.com/img/userinfo.gif" inside the altered text to "
http://pics.livejournal.com/img/fb-userinfo.gif" thus changing to
![](http://pics.livejournal.com/img/fb-userinfo.gif)
.
append_arg(string url, string arg) : string
cmshaw's
wide linesfunction append_arg(string url, string arg) : string {
# guts borrowed from widelines (id #4162751)
var string anchor = "";
var string temp = "";
var string joiner = "";
# use ? or & to append the argument
$joiner = $url->contains("?") ? "&" : "?";
# check for anchor link
if ($url->contains("#")) {
foreach var int a (0 .. $url->length() - 1) {
$temp = $url->substr($a, $url->length() - $a);
if ($temp->starts_with("#")) {
# pull anchor string off the end of the url
$anchor = $temp;
$url = $url->substr(0, $a);
}
}
}
# return the url with argument inserted
return "$url$joiner$arg$anchor";
}
quicksort(string[] list, int start, int end) : string[]
quicksort(string[] list) : string[]
by Tony Chang
###############
# #
###############
#// These two functions perform sorting capabilities
#//
#// Credit goes to 'Tony Chang '
#// See:
http://www.livejournal.com/customize/advanced/layersource.bml?id=54714#// % Partitions then recursively calls.
#// % Tested up to 172 sortable elements in a string array
function quicksort(string[] list, int start, int end) : string[] {
if ($end <= $start) {
return $list;
}
#// Pick a pivot and move to the front
var int pivot = rand($start, $end);
var string tmp = $list[$start];
$list[$start] = $list[$pivot];
$list[$pivot] = $tmp;
#// Now Partition
var int left = ($start + 1);
var int right = $end;
foreach var int i ($start .. ($end - 2)) {
if ($list[$left] > $list[$start]) {
#// Swap the left and the right, then move the right back
$tmp = $list[$left];
$list[$left] = $list[$right];
$list[$right] = $tmp;
$right--;
} else {
$left++;
}
}
# put the pivot back in the middle
if ($list[$start] < $list[$left]) {
#// Swap $start and ($left - 1)
$tmp = $list[$start];
$list[$start] = $list[($left - 1)];
$list[($left - 1)] = $tmp;
$pivot = ($left - 1);
} else {
#// Swap $start and $left
$tmp = $list[$start];
$list[$start] = $list[$left];
$list[$left] = $tmp;
$pivot = $left;
}
#// Sort either side of the pivot
$list = quicksort($list, $start, ($pivot - 1));
$list = quicksort($list, ($pivot + 1), $end);
return $list;
}
#// Recursing Function
function quicksort(string[] list) : string[] {
return quicksort($list, 0, size $list - 1);
}
################
# #
################
rehtml(string text) : string "Unescape html in \$text by replacing > with > and < with <."
developed by
masterslacker using
mageboltrat's
replace_textfunction rehtml(string text) : string "Unescape html in \$text by replacing > with > and < with <." {
####################################################################
# by lj:masterslacker using lj:mageboltrat's replace_text function #
####################################################################
return replace_text(replace_text($text, "<", "<"), ">", ">");
}example: if html is entered into the customization wizard it is stored inside the property in its escaped form. this function will unescape it.
remove_arg(string url, string argitem) : string "Remove a query string argument from a URL when given URL and key to remove."
cmshaw's
wide linesfunction remove_arg(string url, string argitem) : string {
# guts borrowed from widelines (id #4162751)
var string temp1 = "";
var string temp2 = "";
var string temp3 = "";
var string url1 = "";
var string url2 = "";
var string joiner = "";
# check for item
if (not $url->contains($argitem)) { return $url; }
# break url around argument
foreach var int i (0 .. $url->length() - 1) {
$temp1= $url->substr($i, $argitem->length());
if ($temp1== $argitem) {
# found the argument we're looking for -- put everything prior to it into $url1
$url1 = $url->substr(0, $i - 1);
$joiner = $url->substr($i - 1, 1);
# look at what's after it
$temp2 = $url->substr($i + $argitem->length(), $url->length() - ($i + $argitem->length()));
if ($temp2 ->contains("&") or $temp2 ->contains("#")) {
foreach var int j (0 .. $temp2 ->length() - 1) {
$temp3= $temp2->substr($j, $temp2->length() - $j);
if ($temp3->starts_with("&") or $temp3->starts_with("#")) {
# found another argument or an anchor -- put it and what follows into $url2
# because we can't break the for loop, check that it's the first one
if ($url2 == "") {
if ($temp3->starts_with("&") and $joiner == "?"){
# We removed the first argument and need to bump up the second here
$temp3 = $joiner + $temp3->substr(1, $temp3->length() - 1);
}
$url2 = $temp3;
}
}
}
} else {
# everything after is just the rest of the to-be-removed argument
$url2 = "";
}
}
}
# return everything before and after the argument
return $url1 + $url2;
}
replace_text(string text, string find, string replacement) : string "Search within \$text and replaces any occurences of \$find with \$replacement."
mageboltrat's
industrial nipponfunction replace_text(string text, string find, string replacement) : string "Search within \$text and replaces any occurences of \$find with \$replacement." {
#######################################################
# from lj:mageboltrat's industrial nippon (id=533012) #
#######################################################
var string[] findarray;
var int pos = 1;
foreach var string findcharacter($find) {
$findarray[$pos] = $findcharacter;
$pos = $pos + 1;
}
var int place = 1;
var bool found = false;
var string completedtext = "";
var string foundtext = "";
foreach var string searchcharacter($text) {
if ($searchcharacter == $findarray[$place]) {
if ($place == $find->length()) {
$foundtext = $replacement;
$place = 1;
} else {
$foundtext = $foundtext + $searchcharacter;
$place = $place + 1;
}
} else {
$completedtext = $completedtext + $foundtext + $searchcharacter;
$foundtext = "";
$place = 1;
}
}
$completedtext = $completedtext + $foundtext;
return $completedtext;
}example:
var string replaced_text = replace_text(
$e.text,
"
http://stat.livejournal.com/img/userinfo.gif",
"
http://pics.livejournal.com/img/fb-userinfo.gif"
);
split_left (string input, string break) : string "Split a string on a delimiter once and return first half"
by
mageboltratfunction split_left (string input, string break) : string "Split a string on a delimiter once and return first half" {
#####################
# by lj:mageboltrat #
#####################
$input = reverse $input;
var string results;
var bool found;
$found = false;
$results = "";
foreach var string character ($input) {
if ($found == true) { $results = $results + $character; }
if ($character == $break) { $found = true ; }
}
$results = reverse $results;
return $results;
}example:
var string string = "foo|bar|moo";
var string left = split_left($string); # yields "foo"
split_right (string input, string break) : string "Split a string on a delimiter once and return second half"
by
mageboltratfunction split_right (string input, string break) : string "Split a string on a delimiter once and return second half" {
#####################
# by lj:mageboltrat #
#####################
$input = reverse $input;
var string results;
var bool found;
$found = false;
$results = "";
foreach var string character ($input) {
if ($character == $break) { $found = true ; }
if ($found == false) { $results = $results + $character; }
}
$results = reverse $results;
return $results;
}example:
var string string = "foo|bar|moo";
var string right = split_right($string); # yields "bar|moo"
split (string input, string break) : string[] "Split a string on a delimiter once and return array of two strings"
by
mageboltratfunction split (string input, string break) : string[] "Split a string on a delimiter once and return array of two strings" {
#####################
# by lj:mageboltrat #
#####################
var string[] output;
$output[0] = split_left($input, $break);
$output[1] = split_right($input, $break);
return $output;
}example:
var string string = "foo|bar|moo";
var string[] split = split($string); # yields array("foo", "bar|moo");
split_mult (string input, string break) : string[] "Split a string on a delimiter multiple times and return array of split strings"
by
kunzite1 based upon
mageboltrat's split_left, split_right, split
function split_mult (string input, string break) : string[] "Split a string on a delimiter multiple times and return array of split strings" {
#############################################################################
# by lj:kunzite1 based upon lj:mageboltrat's split_left, split_right, split #
#############################################################################
var string results;
var string[] output;
$output[0] = "";
var int found;
var int count;
$results = "";
$found = 0;
$count = 0;
foreach var string character ($input) {
if ($found == 1) { $found = 0; }
if ($character == $break) { $output[$count] = $results; $results = ""; $count = $count + 1; $found = 1; }
if ($found == 0) { $results = $results + $character; }
}
$output[$count] = $results;
return $output;
}example:
var string string = "foo|bar|moo";
var string[] split = split_mult($string); # yields array("foo", "bar", "moo");
split2(string str, string delim) : string[]
by
kumba12345.
function split2(string str, string delim) : string[] "Split a string on a multiple-character delimiter" {
####################
# by lj:kumba12345 #
####################
var string[] list = [""]; #// List that holds parsed elements
var string bit = ""; #// Holds $delimsize-character bits of the string at a time
var int strsize = $str->length(); #// String Length
var int delimsize = $delim->length(); #// Delimiter length
var int i = 0; #// List Position
var int p = 0; #// Character Position in String
var int a = 0; #// Start Position of Chunk
var int z = 0; #// End Position of Chunk
var bool end = false; #// Bool to determine end of string
#// If passed a valid string and delimiter (size > 0), do our magic
if(($strsize > 0) and ($delimsize > 0)) {
#// Starting off, see if the string even contains a delimiter
if(not $str->contains($delim)) {
#// It doesn't, just return the whole string
$list[0] = $str;
return $list;
}
#// Assign entire string to $chunk
var string chunk = $str;
#// Loop
foreach $p (0 .. ($strsize - 1)) {
#// Get a block of characters, up to $delimsize, and store to $bit
$bit = $str->substr($p, $delimsize);
#// Keep checking each chunk for delimiters until the end of the string
if (not $end) {
#// Does bit match delimiter?
if ($bit == $delim) {
#// It does
#// Calculate the end position of the chunk
$z = ($p - ($delimsize - 1));
#// Get chunk w/o delimiter
$list[$i] = $str->substr($a, (($z - $a) + 1));
$i++;
#// Calculate the start position of the next chunk
$a = ($p + $delimsize);
#// Reset $chunk to contain the remainder of the string
$chunk = $str->substr($a, ($strsize - $a));
#// Does the new chunk have any more delimiters?
#// If it does, not at end of string yet.
#// If it doesn't, at end of string, make final assignment.
if (not $chunk->contains($delim)) {
$end = true;
$list[$i] = $chunk;
}
}
}
}
}
#// All done, return the split list
return $list;
}example:
var string string = "foo||bar||moo";
var string[] split = split2($string); # yields array("foo", "bar", "moo");
striphtmlnew(string in, int maxlen) : string "Strip HTML from text"
by
idealismsnote: striphtml(string s) does function in the core.
function striphtmlnew(string in, int maxlen) : string "Strip HTML from text"
###################
# by lj:idealisms #
###################
{
var string ans;
var int inside = 0;
var int len = 0;
foreach var int pos (0 .. $in->length()) {
if ("<" == $in->substr($pos, 1)) {
$inside = $inside + 1;
}
if (0 == $inside) {
$ans = $ans + $in->substr($pos, 1);
$len = $len + 1;
}
if (">" == $in->substr($pos, 1) and $inside > 0) {
$inside = $inside - 1;
}
if ($maxlen == $len) {
return $ans+"...";
}
}
return $ans;
}
function striphtmlnew(string in) : string "Strip HTML from text"
###################
# by lj:idealisms #
###################
{
return striphtmlnew($in, $in->length());
}example:
var string stripped_text_short = striphtmlnew($e.text, 50);
var string stripped_text = striphtmlnew($e.text);
strstr(string needle, string haystack) : string
by
kunzite1. requested by
senji.
function strstr(string needle, string haystack) : string {
#############################################################################################
# by lj:kunzite1. requested by lj:senji. #
#
http://www.opengroup.org/onlinepubs/009695399/functions/strstr.html #
#
http://www.php.net/manual/en/function.strstr.php #
# return null string if needle not found in haystack #
# if haystack is null string, return needle #
# if needle is found in haystack, return end of haystack, starting from beginning of needle #
#############################################################################################
var string return = "";
var bool found = false;
if($haystack->length() > 0) {
if($haystack->contains($needle)) {
foreach var int pos (0 .. size($haystack)) {
if(($haystack->substr($pos, $needle->length()) == $needle) and not $found) {
$return = $haystack->substr($pos, $haystack->length() - $pos);
$found = true;
}
}
}
} else {
$return = $needle;
}
return $return;
}example:
var string needle = "";
var string haystack = "";
var string output = "";
"""needlehaystackoutput""";
$needle = "@";
$haystack = "user@example.com";
$output = strstr($needle, $haystack);
"""$needle$haystack$output""";
$needle = "@exa";
$haystack = "user@example.com";
$output = strstr($needle, $haystack);
"""$needle$haystack$output""";
$needle = "@";
$haystack = "user@example@com";
$output = strstr($needle, $haystack);
"""$needle$haystack$output""";
$needle = "@exa";
$haystack = "user@example@com";
$output = strstr($needle, $haystack);
"""$needle$haystack$output""";
$needle = "foo";
$haystack = "bar";
$output = strstr($needle, $haystack);
"""$needle$haystack$output""";
$needle = "@";
$haystack = "";
$output = strstr($needle, $haystack);
"""$needle$haystack$output""";
$needle = "@exa";
$haystack = "";
$output = strstr($needle, $haystack);
"""$needle$haystack$output""";
$needle = "";
$haystack = "@";
$output = strstr($needle, $haystack);
"""$needle$haystack$output""";
$needle = "";
$haystack = "@exa";
$output = strstr($needle, $haystack);
"""$needle$haystack$output""";
$needle = "";
$haystack = "";
$output = strstr($needle, $haystack);
"""$needle$haystack$output""";
"";output:needlehaystackoutput@user@example.com@example.com@exauser@example.com@example.com@user@example@com@example@com@exauser@example@com@example@comfoobar@@@exa@exa@@exa