Solved

Php string function

Posted on 2016-10-24
5
30 Views
Last Modified: 2016-11-12
i have a string which has 7 letters

I want to change the first two letters of this strings if it's first two letter is "st"

How can do it with php?
0
Comment
Question by:Braveheartli
5 Comments
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41858052
try this:
<?php

$yourStr = "stllo12";
$findStr = "st";
$replaceStr = "bo";

if (substr($yourStr,0,2) == $findStr) {
    $yourStr = str_replace($findStr,$replaceStr,$yourStr);
}

echo substr($yourStr,0,2)." ".$findStr." ".$yourStr;

?>

Open in new window

0
 
LVL 1

Author Closing Comment

by:Braveheartli
ID: 41858057
thank you
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41858087
Alternative

var $result = preg_replace('/^[s|S][t|T]/', 'bo', $str);

Will match any combination of case for 'st' but will replace with lower case.
Test case
<?php
$tests = array (
  "street",
  "sTreet",
  "treet",
  "sstreet",
  "STREET",
  "astreet",
  "Street",
  "avast",
  "harvester"
);
foreach($tests as $t) {
	echo "Original: [{$t}] - Replace [" . preg_replace('/^[s|S][t|T]/', 'bo', $t) . "]<br/>";
}

Open in new window

Result
Original: [street] - Replace [boreet]
Original: [sTreet] - Replace [boreet]
Original: [treet] - Replace [treet]
Original: [sstreet] - Replace [sstreet]
Original: [STREET] - Replace [boREET]
Original: [astreet] - Replace [astreet]
Original: [Street] - Replace [boreet]
Original: [avast] - Replace [avast]
Original: [harvester] - Replace [harvester]

Open in new window

1
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41884690
This may not be part of the question, but it's worth knowing.

Search this page for "English ordinal suffix"
0
 
LVL 1

Author Comment

by:Braveheartli
ID: 41884864
Thank you Ray,
You are the man.
You are great, Sage, Genius.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

747 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

11 Experts available now in Live!

Get 1:1 Help Now