Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Php string function

Posted on 2016-10-24
5
Medium Priority
?
65 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 53

Accepted Solution

by:
Ryan Chong earned 2000 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 59

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 111

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

704 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