473,842 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 100101
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*********@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-----
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
2332
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 = strXSLT & "<xsl:if test='starts-with(./@title,L1)'>" strXSLT = strXSLT & "<xsl:value-of select='substring(./@title, 4, 10)'
1
4220
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 work? Or it will also match "Ni"? author A more general question, how do a match against a regular expression? Thanks Li Xuejun
1
3543
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" /> </folder> <folder name="en"> <folder name="_images">
6
8442
by: garyusenet | last post by:
Hello, Can someone tell me how to check if a string variable starts with a dash. ' - ' Thanks, Gary.
2
1801
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 security=SSPI;database=NDD"/>
3
30886
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
2432
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
12645
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: open( FILE, "<$file" ); open( COPIED_FILE, ">$copiedfile" ); while( <FILE> ) { $templine = $_; if ($templine =~ m/DELTA\//)
3
1907
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 random import randrange #Simulates a fight monsters =
0
9870
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9715
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10942
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10610
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10310
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7035
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5884
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4088
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3142
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.