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

  • Home
  • SEARCH
  • 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 8639713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:05:28+00:00 2026-06-12T11:05:28+00:00

Possible Duplicate: Preg Replace – replace second occurance of a match I have a

  • 0

Possible Duplicate:
Preg Replace – replace second occurance of a match

I have a string that includes the word rules twice. I need to find and replace the 2nd word. Tried fooling around with str_replace() but couldn’t get anything, the 4th parameter wasn’t what I expected.

Here is an example string:

http://localhost/proj1/modstart/admin/index.php?i=rules&sid=397ab1f6b8eb8a17787438a7e2e60ea3&mode=rules

After my replace it should look like this:

http://localhost/proj1/modstart/admin/index.php?i=rules&sid=397ab1f6b8eb8a17787438a7e2e60ea3&mode=manage

I read that preg_replace() could help, but I don’t know how to write patterns.

Ideas?

P.S: Don’t suggest splitting the string into two variables, that wouldn’t serve my needs.

  • 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-12T11:05:29+00:00Added an answer on June 12, 2026 at 11:05 am

    It would be a very good idea to learn about regular expressions. In PHP, you can accomplish your find/replace like this:

    $result = preg_replace('/(rules.*?)rules/','$1manage',$str,1);
    

    It basically finds “rules” once, then anything, then rules a second time, then puts it all back before the second match and replaces the word.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Replace only first match using preg_replace I have a string as follows:
Possible Duplicate: PHP Strip_tags for div with a specific ID? I have a string
Possible Duplicate: Converting ereg expressions to preg I have this ereg() expression: ^[0-9]{8}\T{1}[0-9]{2}\:[0-9]{2}\:[0-9]{2}$ How
Possible Duplicate: Regex / Preg: No match, if found I want to use preg_replace
Possible Duplicate: PHP code to remove everything but numbers i have a loop that
Possible Duplicate: How to Truncate a string in PHP to the word closest to
Possible Duplicate: regexp with russian lang I have a regular expression that filters out
Possible Duplicate: PHP - remove <img> tag from string I have my content like
Possible Duplicate: PHP preg_split string into letter pairs I have an string looking like
Possible Duplicate: preg_replace all but numbers, letters, periods, and slash? I have a string

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.