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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:20:35+00:00 2026-05-25T02:20:35+00:00

While delving into some old code I’ve stumbled upon a function which is used

  • 0

While delving into some old code I’ve stumbled upon a function which is used to clean up special characters using preg_replace very excessively. Unfortunatly there’s no way to avoid this cleanup, the messed up data arrive from outside.

/* Example 1: current code */
$item = preg_replace('/' . chr(196) . '/',chr(142),$item);
$item = preg_replace('/' . chr(214) . '/',chr(153),$item);
$item = preg_replace('/' . chr(220) . '/',chr(154),$item);

Alot of those lines are in there, and all do the same (using different characters), so there should be a better way to do this. My first iteration to optimize this would be to use an indexed array, like this:

/* Example 2: slightly optimized code */
$patterns = array();
$patterns[0] = '/'.chr(196).'/'; 
$patterns[1] = '/'.chr(214).'/';
$patterns[2] = '/'.chr(220).'/';

$reps = array();
$reps[0] = chr(142); 
$reps[1] = chr(153);
$reps[2] = chr(154);

$item = preg_replace($patterns,$reps,$item);

It does the job, but I guess there’s somewhere a better and/or faster way of doing this alot easier and smarter – maybe even without preg_replace. Due to the early morning and/or the lack of good coffee, I was unable to find it myself so far.

Any suggestions?

  • 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-25T02:20:36+00:00Added an answer on May 25, 2026 at 2:20 am

    As far as you’re concerned to replace characters, you can make use of strtrDocs which has a nice array parameter:

    $map = array(
      chr(196) => chr(142), 
      chr(214) => chr(153),
      chr(220) => chr(154),
    );
    
    $item = strtr($item, $map);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While revising some old c++ code, I ran across several bitflags defined as enums.
Im slowly delving into Silverlight and after a good while trying I am finally
While porting some code from Delphi 7 to Delphi 2010 I was rewriting my
While trying to compile the following code, which is enhanced version of read build
While cross-site scripting is generally regarded as negative, I've run into several situations where
While I search this one out on my own, may be some one could
While browsing through source code in my IDE I'll sometimes wish I could see
While creating a simple client for a REST service which I have stubbed out,
While browsing the source code of the Scala API, I met this package definition
While declaring a simple UIAlertView, with the following code, I get the above mentionned

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.