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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:58:51+00:00 2026-05-11T09:58:51+00:00

In PHP, I want to encode ampersands that have not already been encoded. I

  • 0

In PHP, I want to encode ampersands that have not already been encoded. I came up with this regex

/&(?=[^a])/ 

It seems to work good so far, but seeing as how I’m not much of a regex expert, I am asking if any potential pitfalls can be seen in this regex?

Essentially it needs to convert & to & but leave the & in & as is (so as not to get &)

Thanks

Update

Thanks for the answers. It seems I wasn’t thinking broadly enough to cover all bases. This seems like a common pitfall of regexs themselves (having to think of all possibilities which may make your regex get false positives). It sure does beat my original one str_replace(' & ', ' & ', $string); 🙂

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T09:58:51+00:00Added an answer on May 11, 2026 at 9:58 am

    Even better would be negative lookahead assertion to verify & isn’t followed by amp;

    /&(?!amp;)/ 

    Though that will change any ampersands used for other entities. If you’re likely to have others, then how about something like

    /&(?!#?[a-zA-Z0-9]+;)/ 

    This will look for an ampersand, but asserting that it is NOT followed by an optional hash symbol (for numeric entities), a series of alphanumerics and a semicolon, which should cover named and numeric entities like &quote; or ª

    Test code

    $text='It’s 30 ° outside & very hot. T-shirt & shorts needed!';  $text=preg_replace('/&(?!#?[a-z0-9]+;)/', '&', $text);  echo '$text\n'; 

    Which will output

    It’s 30 ° outside & very hot. T-shirt & shorts needed! 

    which is more easily read as ‘It’s 30 ° outside & very hot. T-shirt & shorts needed!’

    Alternative for PHP 5.2.3+

    As Ionut G. Stan points out below, from PHP 5.2.3 you can use htmlspecialchars with a fourth parameter of false to prevent double-encoding, e.g.

    $text=htmlspecialchars($text,ENT_COMPAT,'UTF-8',false); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a big php code that i want to encode and decode it
I am working with php and want to have a page that has a
I have this string in php i want to skip the ppattern ------------------------------------------ FROM:Andy;
$str = preg_replace('\(look: (.{1,80})\)'Ui, (look: <a href=\dict.php?process=word&q=\\1\>\\1</a>),$str); i want to encode url, but how
I have the following JSON string, encoded with PHP 5.2 json_encode(): {foo:\\.} This JSON
I have a JSON string (from PHP's json_encode() that looks like this: [{id: 1,
I have a PHP data structure I want to JSON encode. It can contain
I want to encode binary file into base64 and put it into php page
I want to parse a X.509 certificate in php. The certificate is in DER-encoded
I have built a site using php and want to try keep it one

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.