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

Php string function

Posted on 2016-10-24
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?
Question by:Braveheartli
LVL 51

Accepted Solution

Ryan Chong earned 500 total points
ID: 41858052
try this:

$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


Author Closing Comment

ID: 41858057
thank you
LVL 55

Expert Comment

by:Julian Hansen
ID: 41858087

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
$tests = array (
foreach($tests as $t) {
	echo "Original: [{$t}] - Replace [" . preg_replace('/^[s|S][t|T]/', 'bo', $t) . "]<br/>";

Open in new window

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

LVL 109

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"

Author Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
PHP query / monitor data from Telnet to MySQL 7 50
website maintenance mode 1 26
Email called spam 5 36
Help with PHP 13 27
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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.
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 look for a specific file type in a local or remote server directory using PHP.

828 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