Solved

Php string function

Posted on 2016-10-24
5
25 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 54

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 54

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 54

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP installation issues 11 59
hosting images 4 27
Wordpress Taxonomy 2 27
Insert won't work due to space in column name 5 20
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

786 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