Skip to content

Instantly share code, notes, and snippets.

@un33k
Created September 5, 2012 17:56
Show Gist options
  • Save un33k/3641275 to your computer and use it in GitHub Desktop.
Save un33k/3641275 to your computer and use it in GitHub Desktop.
Some sample code in PHP
<?php
// truncate string to specified length with ... at end
//////////////////////////////////////////////////////////////
function jaStrTrunc($str, $size)
{
$slen = strlen($str);
if ($slen < $size){
return $str;
}
$str .= " ";
$str = trim(substr($str, 0, $size-strlen(" ... ")));
return $str . " ...";
}
// truncate string to specified length with no ... at end
//////////////////////////////////////////////////////////////
function jaStrCut($str, $size)
{
$slen = strlen($str);
if ($slen < $size){
return $str;
}
$str = trim(substr($str, 0, $size));
return $str;
}
// highlight array of words in string
// if $StyleIfMatch = false, then we skip styling if string == keywords
//////////////////////////////////////////////////////////////
function jaHighLight($string, $keywords, $styleBefore, $styleAfter, $StyleIfMatch = true)
{
if(strtoupper($string) == strtoupper($keywords[0]) && !$StyleIfMatch){
return $string;
}
if(!empty($keywords))
{
foreach($keywords as $keyword)
{
$keyword = preg_quote($keyword);
$string = highligh_word_case($string, $keyword);
}
$codes = array("11#11");
$tags = array($styleBefore);
$string = str_replace($codes, $tags, $string);
$codes = array("22#22");
$tags = array($styleAfter);
$string = str_replace($codes, $tags, $string);
}
return $string;
}
// highlight a single word in string keep the case
function highligh_word_case($haystack,$needle)
{
$h=strtoupper($haystack);
$n=strtoupper($needle);
if($n == "OR"){
// reserved word, spkip
return $haystack;
}
$pos=strpos($h,$n);
if ($pos !== false)
{
$var=substr($haystack,0,$pos)."11#11".substr($haystack,$pos,strlen($needle))."22#22";
$var.=substr($haystack,($pos+strlen($needle)));
$haystack=$var;
}
return $haystack;
}
// double qoute handler
// take a string and retunrs and array with anything in double qoute as single string
function jaDoubleQouteArray($str)
{
// first replace any -"multi words" with "+multi words";
$str = trim(ereg_replace(" +", " ", $str));
$str = str_replace('-"', '"-', $str);
$expr="/ (?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/";
$results=preg_split($expr,trim($str));
return preg_replace("/^\"(.*)\"$/","$1",$results);
}
function jaHandleAndOrExact($keyArray){
$InvalidIndex = "ADFSASDSAwerqweradsfasdf";
$OR_ED_Index = "Already-OR-edADFSASDSAwerqweradsfasdf";
$arrAND = array();
$arrOR = array();
$arrNOT = array();
// clean up
$keyCount = count($keyArray);
for($iI=0; $iI<$keyCount; $iI++){
$keyArray[$iI] = trim($keyArray[$iI]);
}
// make sure we don't start with OR
if(strtoupper($keyArray[0]) == "OR"){
$keyArray[0] = $InvalidIndex;
}
if(strtoupper($keyArray[$keyCount-1]) == "OR"){
$keyArray[$keyCount-1] = $InvalidIndex;
}
// take out the NOT words
for($iI=0; $iI<$keyCount; $iI++){
if($keyArray[$iI][0] == "-"){
$arrNOT[$keyArray[$iI]] = trim($keyArray[$iI],"-");
$keyArray[$iI] = $InvalidIndex;
}
}
// take out the OR words Hello OR Hi OR Hey
for($iI=0; $iI<$keyCount; $iI++){
if(strtoupper($keyArray[$iI]) == "OR"){
if($keyArray[$iI-1] != $InvalidIndex && $keyArray[$iI+1] != $InvalidIndex){
if($keyArray[$iI-1] != $OR_ED_Index){
$arrOR[$keyArray[$iI-1]] = $keyArray[$iI-1];
$keyArray[$iI-1] = $OR_ED_Index;
}
if($keyArray[$iI+1] != $OR_ED_Index){
$arrOR[$keyArray[$iI+1]] = $keyArray[$iI+1];
$keyArray[$iI+1] = $OR_ED_Index;
}
}
$keyArray[$iI] = $OR_ED_Index;
}
}
// take out And keywords
foreach($keyArray as $And){
if($And != $InvalidIndex && $And != $OR_ED_Index){
$arrAND[$And] = $And;
}
}
$arrSorted = array();
$arrSorted["AND"] = $arrAND;
$arrSorted["OR"] = $arrOR;
$arrSorted["NOT"] = $arrNOT;
// print_r($arrSorted);
return $arrSorted;
}
// short form of htmlentities
function jaHE($str)
{
return htmlentities($str);
}
// remove before/after spaces, only single space allowed
function jaSpaceClean($text){
return ereg_replace(' +', ' ', trim(preg_replace("/(\r\n|\r|\t|\n)/s", '', $text)));
}
// lower the string workds in bigger than 3 char long
function jaLowerStringWords($string){
$output ="";
$str = explode(" ", $string);
foreach($str as $s){
if(strlen($s)>3){
$output .= strtolower($s) . " ";
}else{
$output .= $s . " ";
}
}
return trim($output);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment