Solved

Php string function

Posted on 2016-10-24
5
20 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 53

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 53

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 53

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Permalink issue in Wordpress archive 1 35
Load google maps api into wordpress 5 38
How to count in a table in php 22 19
php construct 5 15
Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
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 …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

863 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

18 Experts available now in Live!

Get 1:1 Help Now