Only read the first character of a string

5
if ($myrow['Noticia'] !== '<' && $myrow['Noticia'] !== 'Nao') {
   echo "Noticia mal colocada!";
}

I have this code and would like to just check the first character of the Noticias column that would be this < and if it did not have this character at the beginning, and was different from No , message.

    
asked by anonymous 01.02.2015 / 21:34

2 answers

8

You can read the first character by passing its index as an array:

$myrow['Noticia'][0]

Note: $myrow['Noticia'][0] will generate a warning ( notice ) if the string value is null.

    
01.02.2015 / 21:45
6

Another alternative is the substr function:

if (substr($myrow['Noticia'], 0, 1) !== '<' && $myrow['Noticia'] !== 'Nao') {
   echo "Noticia mal colocada!";
}

Where substr ( string $string , int $start [, int $length ] ) :

  • $string : The variable to parse.
  • $start : Indicates starting position in $string .
  • $length : The amount of characters that should be returned from $start .
01.02.2015 / 21:46