Teno
Teno

Reputation: 2632

Mark Nth Character in String in PHP

I'm wondering if there is a good way to mark a n th character. 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.
  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.

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.

Upvotes: 1

Views: 172

Answers (2)

venkatKA
venkatKA

Reputation: 2399

Your function may contain this single statement : if($pos<strlen($string) return substr($string,0,$pos-1).'<b><font color="red">' . $string{$pos-1} . '</font></b>'.substr($string,$pos+1)

Upvotes: 1

Teno
Teno

Reputation: 2632

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.

Upvotes: 0

Related Questions