Connecting Tech Pros Worldwide Forums | Help | Site Map

String 'starts with' function

Paul Morrison
Guest
 
Posts: n/a
#1: Mar 9 '06
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
Guest
 
Posts: n/a
#2: Mar 9 '06

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:
http://nl2.php.net/manual/en/function.substr.php

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
Iván Sánchez Ortega
Guest
 
Posts: n/a
#3: Mar 9 '06

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_breakfast@hotmail.com
Jabber:ivansanchez@jabber.org ; ivansanchez@kdetalk.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFED/H63jcQ2mg3Pc8RAvSDAJ9YX11a0R+CQfWmrjUHrPGVePuJQQCe KJSA
fYcQp2s3wr1WjFoNTNX3xJ4=
=/fkx
-----END PGP SIGNATURE-----
Marcin Dobrucki
Guest
 
Posts: n/a
#4: Mar 9 '06

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
Paul Morrison
Guest
 
Posts: n/a
#5: Mar 9 '06

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]


Janwillem Borleffs
Guest
 
Posts: n/a
#6: Mar 9 '06

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


Closed Thread