473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
Mar 9 '06 #1
5 100064
Paul Morrison wrote:
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


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
Mar 9 '06 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Paul Morrison wrote:
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?


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

iD8DBQFED/H63jcQ2mg3Pc8RAvSDAJ9YX11a0R+CQfWmrjUHrPGVePuJQQCe KJSA
fYcQp2s3wr1WjFoNTNX3xJ4=
=/fkx
-----END PGP SIGNATURE-----
Mar 9 '06 #3
Paul Morrison wrote:
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?


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

or whatever you want the string to start with.

/Marcin
Mar 9 '06 #4
Cheers for your help guys, problem solved!

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') ...

Mar 9 '06 #5
Erwin Moller wrote:
if (substr('abcd',0,1) == 'a') ...


Or simply:

$str = 'abcd';

if ($str{0} == 'a') ...
JW
Mar 9 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ragnar Heil | last post by:
Hi, just a simple question, I am not sure about the right way of using quotes within a string I want to delete the first 4 letters of the title if the title begins with "L1" strXSLT =...
1
by: Xuejun Li \(SH/RDC\) | last post by:
Hi, How do I select a value that starts with a certain prefix? For example I want all <author> elements that contain a <last-name> element with a value that begins with the letter Mi. Will this...
1
by: Larry Viezel | last post by:
I have the following XML: <folder name="global"> <folder name="_images"> <file name="creative_apply_txt.jpg" /> <file name="creative_culture_txt.jpg" /> <file name="push_btn_submit.gif" />...
6
by: garyusenet | last post by:
Hello, Can someone tell me how to check if a string variable starts with a dash. ' - ' Thanks, Gary.
2
by: lauralucas | last post by:
Hello I'm trying to use a connection string stored in web.config here is the web.config part <appSettings> <add key="NDDbase" value="Server=SOMESERVER\\SOMEINSTANCE;integrated...
3
by: SoFaraway | last post by:
Dear all, In C, how do we check whether a string starts with a substring? E.g., char *str = ...; char *substr = ...; How do we check whether str starts with substr? Thank you very much!
11
by: Gustaf | last post by:
Some error handling code: catch (System.Xml.XmlException e) { Console.WriteLine("Error in " + e.SourceUri + ": " + e.Message); return; } The output:
2
by: Perl Beginner | last post by:
Hi, Is there a way to find a line in a text file that starts with a forward slash? This is how i find a line in text file that starts with a word and then print that line to another text file: ...
3
by: Indigestion | last post by:
I've started with Python recently, reading a book. Unfortunately, I've come through an error I do now know how to resolve. If you could please help me, it would be greatly appreciated. from...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.