Solved

how can I check for the firs three letters of the word with php?

Posted on 2016-10-20
8
54 Views
Last Modified: 2016-10-20
how can I check for the firs three letters of the word with php?
if my $customerid variable starts with the text "934" I'd like to say "true" the otherwise I'd like to say "false"
0
Comment
Question by:Braveheartli
8 Comments
 
LVL 48

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 125 total points
ID: 41851731
HI,

pls try something like this

if (substr($customerid ,0,3) == "934") echo "yes";

Open in new window

Regards
0
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 250 total points
ID: 41851733
If (substr($customerid, 0, 3) == '934')
  { $something = true; }
else
  { $something = false;}

Edit: posted late :)
0
 
LVL 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 125 total points
ID: 41851735
You probably want to look at substr_compare() that does a comparisson on a substring
if (substr_compare($customerid, "934",0, 3)) {
   // it starts with 934
}

Open in new window

1
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41851740
Follow up (for interest)
substr_compare runs almost in linear time - the length of the string and substring have little impact on running time
the other method increases the longer the strings get.

Consider this sample
<?php
$test = "934373648487548378374238943248726348268234";
$sub = "9343736484875483783742389432487263482682";
$len = strlen($sub);
$start = microtime(true);
for($i = 0; $i < 100000;$i++) {
  if (substr($test, 0, $len) == $sub) {
    $result = true;
  }
  else {
    $result = false;
  }
}
$end = microtime(true);
$one = $end - $start;
$start = microtime(true);
for($i = 0; $i < 100000;$i++) {
  $result = substr_compare($test, $sub, 0, $len);
}
$end = microtime(true);
$two = $end - $start;
echo "One: {$one}<br/>";
echo "Two: {$two}<br/>";

Open in new window

Results for the above
One: 0.15969896316528
Two: 0.019267082214355

Open in new window

With
$sub="934"
One: 0.029422044754028
Two: 0.018805027008057

Open in new window

0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 1

Author Closing Comment

by:Braveheartli
ID: 41851789
thank you all
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41851804
@Braveheartli,

Is there any particular reason you did not choose the first post as the correct answer - it is the same as the accepted post but was there first - by acknowledgement of the second poster?
0
 
LVL 1

Author Comment

by:Braveheartli
ID: 41851809
Dear Julian Hansen,
I'm pretty sure that your answer was correct
I tried yours answer first, but somehow i couldn't get the result...most  probably my fault
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41851840
@Braverhertli,

I was not referring to my answer I was referring to Rgonzo1971.

if (substr($customerid ,0,3) == "934") echo "yes";

Open in new window


This was posted first and is the same as the accepted answer.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now