<?php
/**
 The MIT License

 Copyright (c) 2007 <Tsung-Hao>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 *
 * function KeywordBold($string, $position_array, $bold_tag='b')
 * keyword bolding by position.
 *
 * @author: Tsung <tsunghao@gmail.com>
 * @date: 2007/5/22
 * @version: v1.00
 *
 * @param $string: initial string
 * @param $position: array(0 => array(start_position, end_position), 1 => array(int, int))
 * @return bold string
 *
 * @desc: keyword bold by position.(add <b></b>)
 *   example:
 *     $string = 'test, this is a book, test2, that is.';
 *     $position_array = array(
 *       0 => array (
 *         0 => 0,
 *         1 => 4
 *       ),
 *       1 => array (
 *         0 => 21,
 *         1 => 25
 *       )
 *     );
 *
 *     KeywordBold($string, $position_array) => <b>test</b>, this is a book, <b>test</b>2, that is.
 *
 */
function KeywordBold($string$position_array$bold_tag='b')
{
    if (empty(
$string)) {
        return 
'';
    }

    
$add_position 0;
    for (
$i 0$c count($position_array); $i $c$i++) {
        if (!isset(
$position_array[$i][0]) || !isset($position_array[$i][1]))
            continue;

        if (!
is_numeric($position_array[$i][0]) || !is_numeric($position_array[$i][1]))
            continue;

        if (empty(
$bold_tag))
            
$bold_tag 'b'// <b></b>

        
$bold_len mb_strlen($bold_tag'UTF-8') * 5// 5 = <></>, 2 = 2*b
        
$add_position $bold_len $i// <b></b>

        
$len mb_strlen($string'UTF-8');
        
$pre '';
        
$mid '';
        
$end '';
        
$get_unit $position_array[$i][1] - $position_array[$i][0];

        
// 0 ~ pre-keyword => string ~ pre-keyword
        
$pre mb_substr($string0$position_array[$i][0] + $add_position'UTF-8');

        
// mid => <b>keyword</b>
        
$mid mb_substr($string$position_array[$i][0] + $add_position$get_unit'UTF-8');

        
// pre-keyword ~ end => pre-keyword ~ string
        
$end mb_substr($string$position_array[$i][1] + $add_position$len'UTF-8');

        
$string $pre "<$bold_tag>" $mid"</$bold_tag>" $end;
    }

    return 
$string;
}

/*
// TEST:
$position_array = array(
  0 => array (
    0 => 0,
    1 => 4
  ),
  1 => array (
    0 => 21,
    1 => 25
  )
);
echo KeywordBold('test 一個資料庫可以用, 便 use test (注意這裡不必有分號),', $position_array, 'strong');
*/
?>