Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9046657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:42:23+00:00 2026-06-16T11:42:23+00:00

Let’s say I got this string: $str = alemylaife; (I know it’s misspelled, all

  • 0

Let’s say I got this string:

$str = "alemylaife";

(I know it’s misspelled, all my life)

Now how can I replace

“ale” without replacing whatever comes after “ale” that also contains the same letters?

Let’s say I got this arrays:

$replace = Array("a","l","e");  
$with = Array("wh","at","ever");

$str = str_replace($replace,$with,$str);
echo $str;

this will print:

 whatevermyatwhifever 

How could I stop the str_replace for replacing the letters after “ale”?
Maybe I got to trim the 3 first letters then replacing them then join the trimmed strings?

EDIT: I’m going to try to express myself clearer.
I want only the three (3) first letters to be replaced IF they contain A THEN L THEN whateverletter so, ala ale ald alr aly. Those are the letters that I want to be replaced. I don’t want letters after them to be affected.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T11:42:25+00:00Added an answer on June 16, 2026 at 11:42 am

    Question isn’t really clear. Assuming you want to replace “ale” with “whatever” (correct me if I’m wrong), then you could use preg_replace to limit the number of replacements.

    Example 1: (replace the first occurrence of “ale”)

    preg_replace("/ale/", "whatever", "alemylaife", 1)
    > whatevermylaife
    

    Example 2: (replace only if the string starts with “ale”)

    preg_replace("/^ale/", "whatever", "alemylaife", 1)
    > whatevermylaife
    
    preg_replace("/^ale/", "whatever", "lolalemylaife", 1)
    > lolalemylaife
    

    EDIT: You edited your question to provide better details, I would use:

    // match a string that begins with "al" and then a character (case in-sensitive)
    preg_replace("/^al[a-z]/i", "whatever", "alemylaife");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say you have a string that looks like this: token1 token2 tok3 And
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Let's say I have this string: <div>john doe is nice guy btw 8240 E.
Let me explain best with an example. Say you have node class that can
Let's say I have a table with a Color column. Color can have various
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
let's say.. I have the following java bean. Case1: (Student Bean) Integer id; String
Let suppose I have this class diagram in UML: How can I do, in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.