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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:14:08+00:00 2026-05-25T03:14:08+00:00

I am trying to replace a string from an original text. ( zinc –>

  • 0

I am trying to replace a string from an original text. ( zinc –> zn )

Example: 'zinc zinc zinc zinc3 !zinc zincmatic #zinc zinc9 Zinc @zinc@'

Want: 'zn zn zn zinc3 !zn zincmatic #zinc zinc9 zn @zinc@'

The str_ireplace attempt:

$text = 'zinc zinc zinc zinc3 !zinc zincmatic #zinc zinc9 Zinc @zinc@';
$word = 'zinc';
$attr = 'zn';

// cant str_ireplace now as zincmatic will turn into znmatic and #zinc will turn into #zn
$text = ' '.$text.' '; 
$word = ' '.$zinc.' ';

// will try now
$result = str_ireplace($word, $attr, $word);
echo trim($result);

Prints zn zinc zn zinc3 !zinc zincmatic #zinc zinc9 zn @zinc@. Still have problems as !zinc and second zinc remains due to space problems..

The preg_replace attempt:

$text = 'zinc zinc zinc zinc3 !zinc zincmatic #zinc zinc9 zinc';
$word = 'zinc';
$attr = 'zn';
$result = preg_replace("/\b($word)\b/i",$attr,$text);
echo $result;

Prints zn zn zn zinc3 !zn zincmatic #zn zinc9 zn @zn@
almost got what i want: seems that zinc will turn into zn even if there is some special char near like !zinc or #zinc but not if there is a number zinc9 or text like zincmatic

I just want to put a rule here so that #zinc keeps #zinc, @zinc@ keeps @zinc@ and !zinc turns to !zn

Is there a way to add some exceptions to special chars if zinc is near one of them ( ie : #zinc, zinc#, zinc@, @zinc)

The chars I want to be an execptions are #, &, @

Thanks!

  • 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-25T03:14:09+00:00Added an answer on May 25, 2026 at 3:14 am

    You can define such exceptions with negative assertions. They behave similar to \b and can in fact be used in conjunction.

    In your case you want (?<![#&@]) to probe the preceding character, and (?![#&@]) to test the following character.

    = preg_replace("/(?<![#&@])\b($word)\b(?![#&@])/i",$attr,$text);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying make one replace in string from a array but this dont work
I'm trying to do a Javascript replace (to remove some words from a string)
I'm trying to replace a string, inside of a file, with perl from inside
I am trying to remove / replace white space from a string in as3.
I am trying to replace/modify a part of string in a python file from
I am trying to replace some words from a string and use following code
I am trying to replace/remove this: [\ from string bump3 NSString *bump5 = [bump3
I'm trying to replace all the carriage return characters in a string obtained from
I am trying to replace the backslash (escape) character in a Javascript string literal.
Using the .NET framework, I'm trying to replace double slash characters in 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.