Solved

Php string function

Posted on 2016-10-24
5
58 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 52

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 57

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 110

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

739 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