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 982637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:43:47+00:00 2026-05-16T04:43:47+00:00

I’m just wondering how to apply several rules for a preg_replace without executing them

  • 0

I’m just wondering how to apply several rules for a preg_replace without executing them in the first run. Its a bit complicated let me explain based on an example.

Input:

$string = 'The quick brown fox jumps over the lazy freaky dog'; 

Rules:

  • Replace a, i, o with u (if not at the beginning of a word & if not before/after a vowel)
  • Replace e, u with i (if not at the beginning of a word & if not before/after a vowel)
  • Replace ea with i (if not at beginning of a word)
  • Replace whole words ie dog with cat and fox with wolf (without applying the rules above)

Output:
Thi quick bruwn wolf jimps over thi luzy friky cat

I started with something like that: (Edited thanks to Ezequiel Muns)

$patterns = array();
$replacements = array();

$patterns[] = "/(?<!\b|[aeiou])[aio](?![aeiou])/";
$replacements[] = "u";

$patterns[] = "/(?<!\b|[aeiou])[eu](?![aeiou])/";
$replacements[] = "i";

$patterns[] = '/ea/';
$replacements[1] = 'i';

$patterns[] = '/dog/';
$replacements[0] = 'cat';

echo preg_replace($patterns, $replacements, $string);

Output:

Thi qiick briwn fix jimps ivir thi lizy friiky dig


Edited:

As you can see the problem is that every rule gets overwritten by the previous rule.

Example ‘fox‘:

  1. rule: turns fox into fux
  2. rule: turns fux into fix

Is there a way to avoid the following rule(s) if the character was already been effected by the previous rule?

Does this makes sense?

  • 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-05-16T04:43:47+00:00Added an answer on May 16, 2026 at 4:43 am

    First you need to be explicit about the replacement conditions, your rules say ‘not at the begining of a word and not before/after a vowel’ but you have not implemented that in the regex. You can do this using Negative Lookahead/Lookbehind. For example:

    1. Replace a, i, o with u (if not at the beginning of a word & if not before/after a vowel)

    Can be implemented with:

    $patterns[] = "/(?<!\b|[aeiou])[aio](?![aeiou])/";
    $replacements[] = "u";
    

    This method can be used to implement the first 3 rules.

    The next problem is that ‘fox’ and ‘dog’ will be affected by the first 3 rules, so you should replace the changed version to ‘wolf’ and ‘cat’. So for dog => cat:

    $patterns[] = "/\bdug\b/";
    $replacements[] = "cat";
    

    Note: Because of the way preg_replace works with arrays, it’s much better to not use indexes in the $patterns and $replacements arrays, since these can be misleading. Use the [] operator in pairs like I did above, so you always know what goes with what.

    Part 2:

    Aha. I see. You need to make the replacement exlusive.

    You could use a regex that matches both the first cases, which are the problematic ones. Then you can use an interesting weird feature of preg_replace: When you add the e modifier, the replace string is instead evaluated as PHP code. Combining this with capturing groups, it will allow you to decide whether to output a u or an i according to what you matched.

    $patterns[] = "/(?<!\b|[aeiou])([aeiou])(?![aeiou])/e";
    $replacements[] = '("$1" == "e" || "$1" == "u")? "i":"u"';
    

    *Note the /e and the () around the vowel matching class.

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

Sidebar

Ask A Question

Stats

  • Questions 490k
  • Answers 490k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First of all, it's a really bad idea to use… May 16, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer If you are not dead set on using a listbox,… May 16, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer killproc will terminate programs in the process list which match… May 16, 2026 at 9:17 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a JSP page retrieving data and when single or double quotes are
I'm looking for suggestions for debugging... If you view this site in Firefox or
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

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.