Skip to content

\Xmf\Highlighter::apply() doesn't parse multiple spaces as expected #33

Description

@zyspec

If the input $words string has multiple contiguous spaces the apply() method generates empty $body[] array elements. This has 'minimal' impact other than PHP throwing a warning (Warning: mb_stripos() [function.mb-stripos]: Empty delimiter in ...) but it would be better to use preg_replace instead of str_replace. Just change:

    public static function apply($words, $body, $pre = '<strong>', $post = '</strong>')
    {
        if (!is_array($words)) {
            $words = str_replace('  ', ' ', $words);
            $words = explode(' ', $words);
        }
        foreach ($words as $word) {
            $body = static::splitOnTag($word, $body, $pre, $post);
        }

        return $body;
    }

to:

    public static function apply($words, $body, $pre = '<strong>', $post = '</strong>')
    {
        if (!is_array($words)) {
            $words = preg_replace('/[\s]+/', ' ', $words);
            $words = explode(' ', $words);
        }
        foreach ($words as $word) {
            $body = static::splitOnTag($word, $body, $pre, $post);
        }

        return $body;
    }

There is also a check in the \Highlighter::splitOnTag() method to verify if the mb_substr() function exists. I think this check (and the non-mb_substr() conditional code) can be 'safely' removed since mb_substr() has been included in a standard PHP build for quite some time.

Note: Anyone who is attempting to use the Highlighter class should be aware this implementation requires PHP 5.4.8 or greater.

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions