Solved

Php string function

Posted on 2016-10-24
5
14 Views
Last Modified: 2016-10-24
If my strings first two letters is "st" it's true otherwise it's false.

How can i do it with php?
0
Comment
Question by:Braveheartli
  • 3
  • 2
5 Comments
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41857546
Case sensitive
$result = (substr($str,0,2) == 'st');

Open in new window

Case insensitve
$result = (substr(strtolower($str),0,2) == 'st');

Open in new window

0
 
LVL 1

Author Closing Comment

by:Braveheartli
ID: 41857551
Thank you
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41857555
Working sample
<?php
$tests = array (
  "street",
  "sTreet",
  "treet",
  "sstreet",
  "STREET",
  "astreet",
  "Street"
);
foreach($tests as $t) {
  $caseins =  (substr(strtolower($t),0,2) == 'st');
  $casesen = (substr($t,0,2) == 'st');
  if ($casesen) {
    echo "Case Sensitive match on [<b>{$t}</b>]<br/>";
  }
  if ($caseins) {
    echo "Case Insensitive match on [<b>{$t}</b>]<br/>";
  }
  if (!$caseins && !$casesen) {
    echo "[<b>{$t}</b>] does not start with 'st'<br/>";
  }
}

Open in new window

Output
Case Sensitive match on [street]
Case Insensitive match on [street]
Case Insensitive match on [sTreet]
[treet] does not start with 'st'
[sstreet] does not start with 'st'
Case Insensitive match on [STREET]
[astreet] does not start with 'st'
Case Insensitive match on [Street]

Open in new window

2
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41857556
You are welcome.
0
 
LVL 1

Author Comment

by:Braveheartli
ID: 41857559
Wow, thank you
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

706 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

16 Experts available now in Live!

Get 1:1 Help Now