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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:52:20+00:00 2026-05-16T23:52:20+00:00

How do I: replace characters in a word using preg_replace() but make an exception

  • 0

How do I:

  • replace characters in a word using preg_replace() but make
    an exception if they are part of a
    certain word.
  • replace an uppercase character with an
    uppercase replacement even if the
    replacement is lowercase and vice
    versa.

example:

$string = 'Newton, Einstein and Edison. end';  
echo preg_replace('/n/i', '<b>n</b>', $string); 

from: newton, Einstein and Edison. end
to: Newton, Einstein and Edison. end

In this case I want all the n letters to be replaced unless they are part of the word end And Newton should not change to newton

  • 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-16T23:52:20+00:00Added an answer on May 16, 2026 at 11:52 pm
    echo preg_replace('/((?<!\be)n|n(?!d\b))/i', '<b>\1</b>', $string);
    

    It matches any letter ‘n’ that is either not preceded by [word boundary + e] or not followed by [d + word boundary].

    The general case: /((?<!\b$PREFIX)$LETTER|$LETTER(?!$SUFFIX\b))/i'

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

Sidebar

Related Questions

How can I replace characters with preg_replace , that are enclosed in quotes? I
How to replace special characters using regular expressions? By special, what I mean is
How to replace two characters with one character in a char array? Let me
I am using preg_replace to strip unwanted characters from a string. Thats working fine.
I'm using https://github.com/akzhan/jwysiwyg/ and i want to implement pasting from Word, but I'm not
I am trying to replace a string that is surrounded by non word characters.
I want to replace certain characters in a string with other characters. I´ve did
How can I sanitize URLs made from strings to replace characters like space to
I try to replace few characters in some string 14/04/2010 17:12:11 and get, for
I need to replace accented characters in a string with their unaccented counterparts and

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.