2

I have copied some item manually from web pasted into txt and then stored it into database.Now what I missed is invisible characters.

when I'm retrieving first characters of each word using different values in substr($word,0,x) it shows presence of invisible characters.

php code-

public function getPrefixAttribute()
    {
        $str=$this->attributes['Subject_name'];
        $exclude=array('And', 'of','in');
        $ret = '';
        foreach (explode(' ', $str) as $word)
        {
            if(in_array($word, $exclude)) 
            {continue;}
            else{
            $ret .= strtoupper(substr($word,0,1));}
        }
    return $ret;
    }

output-

substr($word,0,1)
string-'data structures and algorithms'
output-'SA'
expected-'DSA'

string-'Web Development'
output-'WD'

substr($word,0,2)
string-'data structures and algorithms'
output-'DSTAL'
expected-'DASTAL'

string-'Web Development'
output-'WEDE'
Yogesh.galav
  • 225
  • 1
  • 5
  • 17

4 Answers4

0

You are almost there:

<?php
public function getPrefixAttribute()
{
    $str = $this->attributes[ 'Subject_name' ];

    // Make these uppercase for easier comparison
    $exclude = array( 'AND', 'OF', 'IN' );
    $ret = '';
    foreach( explode( ' ', $str ) as $word )
    {
        // This word should have a length of 1 or more or else $word[ 0 ] will fail
        // Check its uppercase version against  the $exclude array
        if( strlen( $word ) >= 1 && !in_array( strtoupper( $word ) , $exclude ) )
        {
            $ret.= strtoupper( $word[ 0 ] );
        }
    }
    return $ret;
}
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
0

You can use the array_ methods to do a lot of the work (details in comments in code)...

public function getPrefixAttribute()
{
    $str=$this->attributes['Subject_name'];
    // Use uppercase list of words to exclude
    $exclude=array('AND', 'OF', 'IN');
    // Split string into words (uppercase)
    $current = explode(" ", strtoupper($str));
    // Return the difference between the string words and excluded
    // Use array_filter to remove empty elements
    $remain = array_filter(array_diff($current, $exclude));

    $ret = '';
    foreach ($remain as $word)
    {
        $ret .= $word[0];
    }
    return $ret;
}

Using array_filter() removes any empty elements, these can cause the [0] part to fail and aren't useful anyway. This can happen if you have double spaces as it will assume an empty element.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
0

Another approach would be to use inbuilt PHP functions:-

function getPrefixAttribute() {
    $str = $this->attributes['Subject_name']; // 'data Structures And algorithms';
    $exclude = array('and', 'of', 'in'); // make sure to set all these to lower case
    $exploded = explode(' ', strtolower($str));

    // get first letter of each word from the cleaned array(without excluded words)
    $expected_letters_array = array_map(function($value){
        return $value[0];
    }, array_filter(array_diff($exploded, $exclude)));

    return strtoupper(implode('', $expected_letters_array));
}
Ashish Choudhary
  • 2,004
  • 1
  • 18
  • 26
0

The invisible characters are '/n','/r','/t' and method for manually removing them is

$string = trim(preg_replace('/\s\s+/', ' ', $string));
Yogesh.galav
  • 225
  • 1
  • 5
  • 17