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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:39:51+00:00 2026-06-13T14:39:51+00:00

For example, the following code wraps each matching character with an <i> tag. echo

  • 0

For example, the following code wraps each matching character with an <i> tag.

echo preg_replace('/[aeiou]/', '<i>$0</i>', 'alphabet');
// result: <i>a</i>lph<i>a</i>b<i>e</i>t

But I’d like it to only replace each character once.

I’m looking for a result like <i>a</i>lphab<i>e</i>t, where the second a makes it through without a tag because the search string only has one a.

Can you help? Is this possible without of iterating through each character in an foreach loop?

The answer should also allow for two or more of the same characters, each only to be used once. For example, if I were searching for aaeioo in the string alphabetsoupisgood, it should match both of the a‘s but only two of the three o‘s.

  • 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-13T14:39:52+00:00Added an answer on June 13, 2026 at 2:39 pm

    If you just want a letter to be replaced only once then following regex should work:

    echo preg_replace('/([aeiou])(?!.+?\\1)/', '<i>$1</i>', 'alphabet');
    

    OUTPUT:

    alph<i>a</i>b<i>e</i>t
    

    PS: Note that it replaces last occurrence of a letter instead of the first one.

    EDIT:

    Following would produce the same output as expected by the OP (thanks to @AntonyHatchkins):

    echo strrev(preg_replace
            ('/([aeiou])(?!.+?\\1)/', strrev('<i>1$</i>'), strrev('alphabet')))."\n";
    

    EDIT 2:

    Upon OP’s comment:

    Can you help me allow more than one a then? How can I match 2, but not 3 a's

    I am posting this answer:

    echo strrev(preg_replace('/([aeiou])(?!(.+?\\1){2})/', 
                strrev('<i>1$</i>'), strrev('alphabetax'))) . "\n";
    

    EDIT 3:

    Upon another of OP’s comment:

    that will allow duplicates for all characters in the string, not just 2 a's & 1 e

    I am posting this answer:

    echo strrev(preg_replace(array('/(a)(?!(.+?\\1){2})/', 
      '/(?<!>)([eiou])(?!.+?\\1)/'), 
      array(strrev('<i>1$</i>'), strrev('<i>1$</i>')), strrev('alphabetaxen')))."\n";
    

    OUTPUT:

    <i>a</i>lph<i>a</i>b<i>e</i>taxen
    

    Note: I believe original problem has already changed so many times so please don’t add further complexity in this problem. You’re free to post another question if you have different queries.

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

Sidebar

Related Questions

Possible Duplicate: C programming division in the following code example the result should yield
For example the following code prints { key: 'b' } function myFunc(key, value) {
In the following code example how do the user/userParam references relate to the Customer
I have the following example code: $dataProvider = new CActiveDataProvider('firstTable', array('criteria' => array( 'select'
Please consider the following example code (from the lm doc): ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt
Please forgive the verbosity of the following code example. Using Delphi 2009, I created
The following code is an example of what I think would qualify as pseudocode,
The following code sample (also at http://jsfiddle.net/MZwBS/ ) var items = []; items.push({ example:
When I execute this following code for example: cart.php?p=1&action=add <?php session_start(); if (isset($_GET['p']) &&
Using the following code: use LWP::Simple; my $url= http://example.com; my $html= get $url; I

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.