|
|||
Hello, my name is Judas Gutenberg and this is my blaag (pronounced as you would the vomit noise "hyroop-bleuach").
linksdecay & ruin got that wrong appropriate tech fun social media stuff Like asecular.com (nobody does!) Like my brownhouse: |
BetterExplode Wednesday, November 28 2007
function BetterExplode($delimiter, $data, $quotechar="\" '", $bwlLeaveQuotesInPlace=false) { //Judas Gutenberg November 28 2007 //moves through data character by character breaking on //delimiters so quoted characters are not mistaken as delimiters //$quotechar can be a space-delimited series of acceptable quotes, //though they cannot be mixed when //quoting a particular string //think of it as a more complicated version of explode //handle quotes being passed in as ascii values: $arrQuotes=explode(" ", $quotechar); $quotechar=""; foreach($arrQuotes as $thisquote) { if(is_numeric($thisquote)) { $quotechar.=chr($thisquote) . " "; } else { $quotechar.=$thisquote. " "; } } $quotechar=RemoveEndCharactersIfMatch($quotechar, " "); //echo "-" . $quotechar . "-"; if (contains($delimiter, ".")) { $delimiter=multichrhandle($delimiter); //hacky and imprecise i know - but for multiple //character delimeters this is what i do! //and it hardly matters in most cases, where the //second character is chr(10) for($i=0; $i<strlen($data); $i++) { $chr=substr($delimiter, $i, 1); if ($i==0) { $delimiter=$chr; } else { //throw away all instances of other //characters in delimiters in $data - //could break things! $data=str_replace($chr, "", $data); } } } else if (is_numeric($delimiter)) { $delimiter=chr($delimiter); } $arrOut=Array(); if($quotechar=="") { $bwlQuoteOn=true; } else { $bwlQuoteOn=false; } $intArrCursor=0; $chrKnownQuote=""; $chrQuoteToBeEscaping=""; for($i=0; $i<strlen($data); $i++) { $chr=substr($data, $i, 1); if($i+1<strlen($data)) { $chrNext=substr($data, $i+1, 1); } else { $chrNext=""; } if( $chr==$delimiter && (!$bwlQuoteOn || $quotechar=="") ) { $arrOut[$intArrCursor]=$thisvar; $intArrCursor++; $thisvar=""; } else if ((($chrKnownQuote=="" && inList($quotechar, $chr) || $chrKnownQuote==$chr)) && !$bwlQuoteOn) { $bwlQuoteOn=true; $chrKnownQuote=$chr; if($bwlLeaveQuotesInPlace) { $thisvar.=$chr; } } else if ($bwlQuoteOn && (($chrKnownQuote=="" && inList($quotechar, $chr) || $chrKnownQuote==$chr))) { //handle internally-escaped quotes, that is, a known //quote escaping one immediately following in data if($chrQuoteToBeEscaping==$chr) { $chrQuoteToBeEscaping=""; $thisvar.=$chr; } else if($chrNext==$chrKnownQuote) { $chrQuoteToBeEscaping=$chr; } else { $bwlQuoteOn=false; $chrKnownQuote=""; if($bwlLeaveQuotesInPlace) { $thisvar.=$chr; } } } else if ($bwlQuoteOn) { $thisvar.=$chr; } else { $thisvar.=$chr; } } $arrOut[$intArrCursor]=$thisvar; return $arrOut; } function multichrhandle($strIn) { $out=""; $arrIn=explode(".", $strIn); foreach($arrIn as $chr) { $out.=chr($chr); } return $out; } function inList($strList, $item, $MatchOnlyType="") //look to see if item is in //the space-delimited strList (similar to my ASP version) { $arrThis=explode(' ', $strList); for ($t=0; $t<count($arrThis); $t++) { $thislen=strlen($arrThis[$t]); if($MatchOnlyType=="last") { if ($arrThis[$t]==substr($item, strlen($item)-$thislen)) { return true; } } else if ($MatchOnlyType=="first") { if ($arrThis[$t]==substr($item, 0,$thislen)) { return true; } } else { if ($arrThis[$t]==$item) { return true; } } } return false; } function RemoveLastCharacterIfMatch($strIn, $chrIn) { $out=$strIn; if (substr($strIn, strlen($strIn)-1, 1) ==$chrIn) { $out= substr($strIn, 0, strlen($strIn)-1); } return $out; } function RemoveFirstCharacterIfMatch($strIn, $chrIn) { $out=$strIn; //echo substr($strIn, 0, 1) . "<br>"; if (substr($strIn, 0, 1) ==$chrIn) { $out= substr($strIn, 1); } return $out; } function RemoveEndCharactersIfMatch($strIn, $chrIn) { $out= RemoveFirstCharacterIfMatch($strIn, $chrIn); $out= RemoveLastCharacterIfMatch($out, $chrIn); return $out; } For linking purposes this article's URL is: previous | next |