String 'starts with' function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Morrison

    String 'starts with' function

    Hi

    I have looked at the PHP string functions and I cant see any function to
    check to see whether a string starts with a certain letter or letters. Is
    there any way of doing this with the built in functions?

    Cheers,

    Paul


  • Erwin Moller

    #2
    Re: String 'starts with' function

    Paul Morrison wrote:
    [color=blue]
    > Hi
    >
    > I have looked at the PHP string functions and I cant see any function to
    > check to see whether a string starts with a certain letter or letters. Is
    > there any way of doing this with the built in functions?
    >
    > Cheers,
    >
    > Paul[/color]

    Hi Paul,

    You can do this in many way, but the easiest way is just get the first
    character of the string using substr(), and compare it to what you want.

    It is here:


    if (substr('abcd', 0,1) == 'a') ...

    (Of course, if you like regexpr you can use them too. search for regexpr at
    www.php.net for regexpr-functions)


    Regards,
    Erwin Moller

    Comment

    • Iván Sánchez Ortega

      #3
      Re: String 'starts with' function

      -----BEGIN PGP SIGNED MESSAGE-----
      Hash: SHA1

      Paul Morrison wrote:
      [color=blue]
      > I have looked at the PHP string functions and I cant see any function to
      > check to see whether a string starts with a certain letter or letters. Is
      > there any way of doing this with the built in functions?[/color]

      Have another look at the substr() function, and at the "==" operator.

      - --
      - ----------------------------------
      Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

      http://acm.asoc.fi.upm.es/~mr/ ; http://acm.asoc.fi.upm.es/~ivan/
      MSN:i_eat_s_p_a _m_for_breakfas t@hotmail.com
      Jabber:ivansanc hez@jabber.org ; ivansanchez@kde talk.net
      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v1.4.2 (GNU/Linux)

      iD8DBQFED/H63jcQ2mg3Pc8RA vSDAJ9YX11a0R+C QfWmrjUHrPGVePu JQQCeKJSA
      fYcQp2s3wr1WjFo NTNX3xJ4=
      =/fkx
      -----END PGP SIGNATURE-----

      Comment

      • Marcin Dobrucki

        #4
        Re: String 'starts with' function

        Paul Morrison wrote:
        [color=blue]
        > I have looked at the PHP string functions and I cant see any function to
        > check to see whether a string starts with a certain letter or letters. Is
        > there any way of doing this with the built in functions?[/color]

        preg_match("/^[a-z]/",$string); // strings starting with a,b,c...,z

        or whatever you want the string to start with.

        /Marcin

        Comment

        • Paul Morrison

          #5
          Re: String 'starts with' function

          Cheers for your help guys, problem solved!

          Paul
          [color=blue]
          > You can do this in many way, but the easiest way is just get the first
          > character of the string using substr(), and compare it to what you want.
          >
          > It is here:
          > http://nl2.php.net/manual/en/function.substr.php
          >
          > if (substr('abcd', 0,1) == 'a') ...[/color]


          Comment

          • Janwillem Borleffs

            #6
            Re: String 'starts with' function

            Erwin Moller wrote:[color=blue]
            > if (substr('abcd', 0,1) == 'a') ...
            >[/color]

            Or simply:

            $str = 'abcd';

            if ($str{0} == 'a') ...


            JW


            Comment

            Working...