note 73979 added to function.stripos
| From: | grf at post dot cz | Date: | Mon, 19 Mar 2007 16:43:51 +0000 |
| Subject: | note 73979 added to function.stripos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-124591@lists.php.net to get a copy of this message | ||
<?php
/***************************************
* SAFE HIGHLIGHT
***************************************/
/**
* function finds and encase every string in a $needleArr array with
* strings $shearLft (from the left side) and $shearRgt (guess from which
* side).
* already encased needles are IGNORED for any other step, so order
* of needles in $needleArr is pretty important.
*
* function is searching needles in case-insensitive mode,
* but case in the subject is saved.
*
* can you do it better? don't ask me, do it.
*
* PHP sux :P * PHP sux :P
*
* @param array $needleArr array of needles
* @param string $shearLft left shear
* @param string $shearRgt right shear
* @param string $subject subject
* @param string $encoding encoding ('utf-8' is default)
* - see code bellow for PHP5.2.0+ code update!
*
* @author griffin
*/
function safeHighlight( $needleArr, $shearLft, $shearRgt, $subject, $encoding = 'utf-8')
{
global $script_after_content;
// encoding
$e = $encoding;
// oh, no needles
if( !is_array( $needleArr))
return $subject;
// empty keys throw-off, only unique, reindex
$nA = array_values(
array_unique(
array_diff( $needleArr, array(''))
)
);
// needle count
if( !($nC = count( $nA)))
return $subject; // nothing to hl
// shear length
if( !(($rLL = mb_strlen( $rL = $shearLft, $e)) + ($rRL = mb_strlen( $rR = $shearRgt, $e))))
return $subject; // no shears
// subject length
if( !($sL = mb_strlen( $s = $subject, $e)))
return null; // empty subject
// masking ~ 0=not changed, 1=changed
$m = str_repeat( '0', $sL);
// loop for each needle
for( $n=0; $n<$nC; $n++)
{
$o = 0; // offset
$nL = mb_strlen( $nA[ $n], $e); // needle length
// if you have PHP 5.2.0+ use:
// while( false === ($p = @mb_stripos( $s, $nA[ $n], $o, $e)))
while( false !== ($p = @stripos( $s, $nA[ $n], $o)))
{
// oh hurrey, needle found on $p position
// is founded needle already modified?
if( $m[ $p])
{
// ai, caramba. already modified, jump over
$o+= $nL;
continue;
}
// explode subject and mask into three parts
// partA|needle|partB
$sE[0] = mb_substr( $s, 0, $p, $e);
$sE[1] = mb_substr( $s, $p, $nL, $e);
$sE[2] = mb_substr( $s, $p+$nL, $sL-$p-$nL, $e);
// mask
// partA|partB (needle not needed)
$mE[0] = mb_substr( $m, 0, $p, $e);
$mE[1] = mb_substr( $m, $p+$nL, $sL-$p-$nL, $e);
// apply shears
$sE[1] = $rL.$sE[1].$rR;
// update sunject length
$sL+= $rLL + $rRL;
// update mask
$m = $mE[0] . str_repeat( '1', $rLL + $nL + $rRL) . $mE[1];
// implode into a subject
$s = implode( $sE);
// increase offset
$o+= $rLL + $nL + $rRL;
} // while()
} // for( $n=0; $n<$nC; $n++)
// oouu yeaaa, kick the subject out of the function
return $s;
} // function safeHighlight()
/***************************************
* END: SAFE HIGHLIGHT
***************************************/
?>
----
Server IP: 212.24.129.110
Probable Submitter: 81.19.10.91
----
Manual Page -- http://www.php.net/manual/en/function.stripos.php
Edit -- https://master.php.net/note/edit/73979
Del: integrated -- https://master.php.net/note/delete/73979/integrated
Del: useless -- https://master.php.net/note/delete/73979/useless
Del: bad code -- https://master.php.net/note/delete/73979/bad+code
Del: spam -- https://master.php.net/note/delete/73979/spam
Del: non-english -- https://master.php.net/note/delete/73979/non-english
Del: in docs -- https://master.php.net/note/delete/73979/in+docs
Del: other reasons-- https://master.php.net/note/delete/73979
Reject -- https://master.php.net/note/reject/73979
Search -- https://master.php.net/manage/user-notes.php