简体   繁体   English

在PHP中的字符串中标记第N个字符

[英]Mark Nth Character in String in PHP

I'm wondering if there is a good way to mark a n th character. 我想知道是否有一个好的方法来标记第n个字符。 Currently I have this code but there are some flaws with it. 目前,我有此代码,但是它有一些缺陷。

$string ='Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque ac arcu sit amet lorem mollis dignissim ac ut metus. Aliquam sed nulla ut risus sollicitudin luctus vitae eget quam. Nam velit diam, ullamcorper id tempus ac, iaculis sed arcu. Sed sed lectus rhoncus leo vehicula accumsan consequat a risus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Proin posuere, diam scelerisque suscipit accumsan, enim tortor lobortis sapien, a faucibus sapien metus eu erat. Phasellus condimentum elit ac nisi fringilla imperdiet. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Mauris ac lectus quis lorem ornare ornare non vel nunc. Fusce et odio at leo interdum cursus eget sed magna. Nam convallis vehicula fermentum. Etiam turpis mauris, convallis et sollicitudin quis, rutrum in sapien. Suspendisse purus eros, sagittis vitae venenatis tincidunt, imperdiet molestie sem. Vestibulum eleifend urna vitae tortor suscipit consequat. Praesent placerat magna a ligula fringilla dapibus. Ut volutpat purus eu felis elementum eu laoreet nulla eleifend.';

echo mark_nth_char($string, 100);
function mark_nth_char($string, $pos, $prefixlen=10, $suffixlen=10, $fixchar='...') {
    $output = $fixchar;
    for ($i = $pos - $prefixlen ; $i < $pos + $suffixlen; $i++ ) {
        if ($i == $pos)
            $output .= '<b><font color="red">' . $string[$i] . '</font></b>';
        else 
            $output .= $string[$i];
    }
    return $output . $fixchar;
}

The problems: 问题:

  1. if the passed number is less than $prefixlen` it causes an error. 如果传递的数字小于$ prefixlen`,则会导致错误。
  2. if the passed number exceeds the total string length, it causes an error. 如果传递的数字超过字符串的总长度,则会导致错误。
  3. if the total string length is less than the sum of $prefixlen and $suffixlen , they should not be added. 如果总字符串长度小于$prefixlen$suffixlen的总和, $suffixlen它们相加。

I'd like to include strings around the matching character (the character with the specified position). 我想在匹配字符(具有指定位置的字符)周围包括字符串。 There might be other issues I'm not aware of but rather than fixing those problems, I'm wondering if there is a better approach for it, which could be done in a more efficient and elegant way. 我可能还没有意识到其他问题,但是除了解决这些问题之外,我想知道是否有更好的方法可以更有效,更优雅地完成。

Thanks for your information. 感谢你的信息。

您的函数可能包含以下单个语句: if($pos<strlen($string) return substr($string,0,$pos-1).'<b><font color="red">' . $string{$pos-1} . '</font></b>'.substr($string,$pos+1)

Okay, this seems to work so far. 好的,到目前为止,这似乎可行。 Please let me know if there is a flaw in it. 请告诉我其中是否有缺陷。 Or any suggestion for improvements would be appreciated. 或任何改进的建议将不胜感激。

$string ='123456789012345678901234567890';
echo '<p>' . mark_nth_char($string, 4) . '</p>';
echo '<p>' . mark_nth_char($string, 15) . '</p>';
echo '<p>' . mark_nth_char($string, 28) . '</p>';

$string = "Hello World.";
echo '<p>' . mark_nth_char($string, 10) . '</p>';
echo '<p>' . mark_nth_char($string, 6) . '</p>';

function mark_nth_char($string, $pos, $prefixlen=10, $suffixlen=10, $fixchar='...') {

    if (!($pos<strlen($string) && $pos > 0)) 
        return;

    // prefix
    $startpos = ($pos < $prefixlen) ? 0 : $pos - $prefixlen ;
    $length = ($pos < $prefixlen) ? $pos - 1 : $prefixlen - 1;
    $prefix = substr($string, $startpos, $length);
    if ($pos > $prefixlen) 
        $prefix = $fixchar . $prefix;

    // match
    $match = '<span style="background-color: FEE333; font-weight:bold; color:red; font-size : xx-large;">' . $string{$pos-1} . '</span>';

    // suffix
    $startpos = $pos;
    $suffix = substr($string, $startpos, $suffixlen);
    if (strlen($suffix) >= $suffixlen) 
        $suffix .= $fixchar;

    // output
    return $prefix . $match . $suffix;

}

I'm also wondering how it could be achieved to pass a negative number for the position so that it finds the character backwards. 我也想知道如何通过传递一个负数的位置,以便倒退找到字符。 But to code it really racks my brain. 但是编写代码确实让我大吃一惊。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM