<?php
/**
* Some utility functions for string handling
*
* The package string_utilities is a functional library of functions and objects to handle
* strings, tags, selections etc.
*
* @author Axel Benz
* @package string_utilities
* @tutorial string_utilities.pkg
*/
/**
* Converts "&" in html entities to "&", so that when rendered in a
* textarea, these entities stay as they are.
*
* @param strin $text
* The text with html entities
* @return string
* The converted text for the textarea.
*/
function preserve_html_entities($text){
$bad_chars=array(
"\xe2\x80\x98", // left single quote
"\xe2\x80\x99", // right single quote
"\xe2\x80\x9c", // left double quote
"\xe2\x80\x9d", // right double quote
"\xe2\x80\x94", // em dash
"\xe2\x80\x93", // en dash
"\xc2\xbb", // right arrow quote
"\xc2\xab", // left arrow quote
"\xc2\xa9", // copyright
"\xc2\xae", // registered
"\xe2\x84\xa2", // trademark
"", // euro
"\xe2\x80\xa2", // bullet
"\xe2\x80\xa6" // elipses
);
$fixed_chars=array(
"‘",
"’",
'“',
'”',
'—',
'–',
'»',
'«',
'©',
'®',
'™',
'€',
'•',
'…'
);
$text = str_replace($bad_chars,$fixed_chars,$text);
#regexp: * zero or one
#(?=x) followed by x, x is not in match
#+: 1 or more
$text = preg_replace('/&(?=#*[0-9a-zA-Z]+;)/', '&', $text);
return $text;
}
/**
* Remove quotes on both sides of a string
*
* @param stringg $inp
* The string with quotes
* @return unknown
* The string without quotes
*
*/
function trim_quotes($inp){
if (substr($inp,0,1)=="\""){$inp=substr($inp,1);}
if (substr($inp,0,1)=="'"){$inp=substr($inp,1);}
if (substr($inp,strlen($inp)-1,1)=="\""){$inp=substr($inp,0,strlen($inp)-1);}
if (substr($inp,strlen($inp)-1,1)=="'"){$inp=substr($inp,0,strlen($inp)-1);}
return $inp;
}
/**
* Returns true if $inp is " or ' (i.e. a quote character)
*
* @param string $inp
* character to investigate
* @return boolean
* true, if $inp is " or ', false otherwise.
*/
function is_quote($inp){
return ($inp=="\"" || $inp=="'");
}
/**
* Returns the rest of $longer if you delete $shorter at the end of $longer.
* The function doesn't care whether $shorter really matches the end of $longer!
*
* @param string $longer
* The string to delete the end of.
* @param string $shorter
* The string to delete at the end of longer.
*/
function str_dif_from_left($longer,$shorter){
return(substr($longer,0,strlen($longer)-strlen($shorter)));
}
/**
* Returns the rest of $longer if you delete $shorter at the beginning of $longer.
* The function doesn't care whether $shorter really matches the beginning of $longer!
*
* @param string $longer
* @param string $shorter
*/
function str_dif_from_right($longer,$shorter){
return (substr($longer,strlen($shorter)));
}
/**
* Returns the part which two strings have in common, starting from right
* @param string str1
* @param string str2
*/
/*
readable, but inefficient version
function common_from_right($str1,$str2){
for ($i=1; $i<=strlen($str1); $i++){
if (substr($str1,-$i,1)!=substr($str2,-$i,1)){
break;
}
}
return substr($str1,-$i+1);
}*/
function common_from_right($str1,$str2){
#unreadable, efficient version
$max_len = max(strlen($str1),strlen($str2));
$str1 = str_pad($str1,$max_len,chr(0),STR_PAD_LEFT);
$str2 = str_pad($str2,$max_len,chr(0),STR_PAD_LEFT);
$fff = str_pad("",$max_len,chr(0xff));
$not_str_2 = $str2 ^ $fff;
$merged = $str1 ^ $not_str_2;
$rest=rtrim($merged,chr(0xff));
$result = substr($str1,strlen($rest));
return $result;
}
/**
* Returns the part which two strings have in common, starting from left
* @param string str1
* @param string str2
*/
function common_from_left($str1,$str2){
for ($i=0; $i<strlen($str1); $i++){
if (substr($str1,$i,1)!=substr($str2,$i,1)){
break;
}
}
return substr($str1,0,$i);
}
/**
* Replace everything which can be a word delimiter with " ".
*
* @param string $inp
* The string to replace delimiters.
* @return string
* The string with " " instead of every other delimiter.
*/
function delimit_words($inp){
#delimit string begin and end
$inp = " ".$inp." ";
#delimit quotes
$inp = str_replace("\""," ",$inp);
$inp = str_replace("'"," ",$inp);
#delimit whitespaces
$inp = str_replace("\n"," ",$inp);
$inp = str_replace("\t"," ",$inp);
#delimit slashes
$inp = str_replace("\\"," ",$inp);
$inp = str_replace("/"," ",$inp);
#delimit less, equal, bigger
$inp = str_replace("="," ",$inp);
$inp = str_replace("<"," ",$inp);
$inp = str_replace(">"," ",$inp);
#delimit commas etc
$inp = str_replace("."," ",$inp);
$inp = str_replace(","," ",$inp);
$inp = str_replace(";"," ",$inp);
#delimit assigment ops
$inp = str_replace(":"," ",$inp);
return $inp;
}
/**
* Overwrite a part of a string on a specific offset
*
* @param string $subject
* The string to overwrite partly.
* @param string $replacement
* The ovewriting string
* @param int $offset
* The offset on which overwrite begins
* @return string
*/
function overwrite($subject,$replacement,$offset){
$beg = substr($subject,0,$offset);
$rest = substr($subject,$offset+strlen($replacement));
return ($beg.$replacement.$rest);
}
/**
* Overwrite several sections of a string.
*
* @param string $inp
* The string to overwrite.
* @param string[][] $replacement
* An array of (replacement, offset) pairs (implemented as array with to 2 elements).
* @return string
*/
function overwrite_all($inp,$replacement){
foreach ($replacement as $entry){
$repl = $entry[0];
$offset = $entry[1];
$inp = overwrite($inp,$repl,$offset);
}
return $inp;
}
/**
* Replace several sections of a string by unique IDs.
*
* @param string $inp
* The string to overwrite.
* @param &string[][] $replacement
* An array of (replacement, offset) pairs (implemented as array with to 2 elements).
* The pairs will get the uniqu id as third element
* @return string
*/
function replace_with_ids($inp,&$replacement){
$out = "";
$pointer = 0;
foreach ($replacement as &$entry){
$repl = $entry[0];
$offset = $entry[1];
$id = "X".md5(uniqid(rand(),true))."X";
$entry[3]=$id;
$out .= substr($inp,$pointer,$offset-$pointer);
$out .= $id;
$pointer = $offset+strlen($repl);
}
$out .= substr($inp,$pointer);
return $out;
}
/**
* Replace IDs of a "replace with ids" operation back
*
* @param string $inp
* The string to perform the replacements in.
* @param array[][] $replacement
* The replacements (@see replace_with_ids())
*/
function replace_ids($inp,$replacement){
foreach ($replacement as $entry){
$inp = str_replace($entry[3],$entry[0],$inp);
}
return $inp;
}
/**
* Deletes \n and \r in a string.
*
* @param string $inp
* @return string
*/
function delete_newlines($inp){
$inp = str_replace("\n","",$inp);
$inp = str_replace("\r","",$inp);
return $inp;
}
/**
* Returns true, if and only if $needle is a stubstring of $haystack
*
* @param string $needle
* @param string $haystack
* @return boolean
*/
function is_substring_of($needle,$haystack){
return (! (strpos($haystack,$needle)===false));
}
/**
* This is a complicated function. As the paths to images can be different on different
* installations (according to their root directory) for testing it makes sense to
* replace a "src="/nabidoo_root/resources/images/button_select_selected.png"" to
* src=button_select_selected.png".
* This is what this function does
*
* @param string $inp
* @return string
*/
function reduce_src_params_to_basename($inp){
return preg_replace("@(src=\")[a-z0-9_\/]*?\/([a-z0-9_\.]*?\")@si","$1$2",$inp);
return $inp;
}
?>