0

Lets say I have:

_Example
_1979

How do we know if the character after _ is a number or a letter? I am not familiar with regex.

This is what I am doing to only find strings starting with _ and then I am removing the _ as I am only using it for other reasons:

if (strpos($txt, '_') !== false) {
   $output = str_replace('_', ' ', $txt);
   echo $output;
  ...Now I should find if the first `character` is a `letter` or a `number`
chris85
  • 23,846
  • 7
  • 34
  • 51
rob.m
  • 9,843
  • 19
  • 73
  • 162
  • 5
    `/^_\d.*$/` will match digits `/^_[a-z].*$/i` will match alpha characters. – CD001 Apr 16 '18 at 14:34
  • @CD001 how would I use that to check if the first character is one or the other then do whatever afterwards? – rob.m Apr 16 '18 at 14:35
  • 2
    `if(preg_match()) { ... }` – CBroe Apr 16 '18 at 14:36
  • 1
    Learn about regular expressions. Try playing with some here [https://regex101.com/](https://regex101.com/) – random_user_name Apr 16 '18 at 14:36
  • Something like `if(preg_match('/^_\d.*$/', $str)) { ... } elseif(preg_match('/^_[a-z].*$/i', $str)) { .... } else { ... doesn't match either ...}` – CD001 Apr 16 '18 at 14:37
  • Ha! magic stackoverflow https://stackoverflow.com/questions/29039472/check-if-first-character-of-a-string-is-letter-or-number-in-php – rob.m Apr 16 '18 at 14:37

3 Answers3

7

If you don't want regex then PHP actually lets you access bytes from a string directly like this:

$string = '_1987';

// Since string indexing is zero-based, we want to check if
// the first char is an underscore and
// the second character is a digit
if( $string[0] === '_' && ctype_digit( $string[1] ) )
{
    echo 'underscore followed by digit!';

    // chop off the leading underscore
    $string = substr( $string, 1 );
}
else
{
    echo 'not underscore followed by digit!';
}

Documentation

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • 1
    `if( ctype_digit( $string[1] ) )` needs to be `if( $string[0] == '_' && ctype_digit( $string[1] ) )` the check needs to be after _ sign – Raymond Nijland Apr 16 '18 at 14:40
  • I will accept this as it isn't using regex and it's more readable for me, yet this question is a duplicate actually since there is one already https://stackoverflow.com/questions/29039472/check-if-first-character-of-a-string-is-letter-or-number-in-php – rob.m Apr 16 '18 at 14:40
  • @RaymondNijland Good call, I missed that tidbit – MonkeyZeus Apr 16 '18 at 14:41
  • Thanks a lot, this is perfect – rob.m Apr 16 '18 at 14:49
  • 1
    @rob.m Glad I could help. Please see my updated answer because the compound `if(){}` block changes what the `else{}` block handles due to Raymond's comment. – MonkeyZeus Apr 16 '18 at 14:52
0

for this you could avoid regexp .. you could simply use is_numeric()

is_numeric(substr($txt, 1));

http://php.net/manual/en/function.is-numeric.php

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

You can do something like

if (is_numeric($txt[1])) ...

You can access the first letter of the string by using square brackets or by using substr, according to your needs.

if (is_numeric(substr($txt, 1))) ...

This will return either true or false

For reference

http://php.net/manual/en/function.is-numeric.php
http://php.net/manual/en/function.substr.php

abr
  • 2,071
  • 22
  • 38
  • he's looking for the character after the _, which is the 2nd character, and since array index starts at 0, the correct position is 1 – abr Apr 16 '18 at 14:42
  • 1
    its cool, I thought the same and only after I've read it again, that I understood properly :P – abr Apr 16 '18 at 14:49