Php string function

Posted on 2016-10-24
Medium Priority
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?
Question by:Braveheartli
  • 3
  • 2
LVL 62

Accepted Solution

Julian Hansen earned 2000 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


Author Closing Comment

ID: 41857551
Thank you
LVL 62

Expert Comment

by:Julian Hansen
ID: 41857555
Working sample
$tests = array (
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

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

LVL 62

Expert Comment

by:Julian Hansen
ID: 41857556
You are welcome.

Author Comment

ID: 41857559
Wow, thank you

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

586 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