Solved

REGEX: Convert & to & only in double entities

Posted on 2008-10-24
6
973 Views
Last Modified: 2009-01-23
Hello,

For security reasons, and to maintain data I now use htmlentities() to clean user-managed settings before placing the values in form input fields.

The problem is that © becomes ©

I wrote a function to fix this but it changes ALL & to & and I only want to change & to & if it is part of an html entity.

So these should be changed
            <
            ©
            ÷
            À
            "
            ©
            ©
            €

But these should NOT be changed:

            This is a test & only a test.
            dsafdsf&adsfdsf
            &€
            &&

function clean_htmlentities ($str) {
return str_replace(array('&','&'),'&',htmlentities($str));
}

Open in new window

0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 22795727
I think you should be using the double_encode in the htmlentities function.  See here for details....

http://us2.php.net/manual/en/function.htmlentities.php
0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 22795743
By the way, double_encode is only available in php 5, so be sure you are up to date in your php version.  :)
0
 
LVL 16

Author Comment

by:hankknight
ID: 22795846
This has to be PHP4 compatible
:-(


0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 22796052
Go to that manual page for htmlentities, and read through the user posted comments below.  You may find some ideas that will help you, such as

http://us2.php.net/manual/en/function.htmlentities.php#70850

http://us2.php.net/manual/en/function.htmlentities.php#48131

I haven't tried any of these, but maybe you can make one of them work for your needs.
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 500 total points
ID: 22804728
the raw regex

(?:&([#x]\d+|[a-zA-Z\d-]+))

then you can prepend the returnd match by &
0
 
LVL 16

Author Comment

by:hankknight
ID: 22825697
How could my function be replaced with this regex?
(?:&([#x]\d+|[a-zA-Z\d-]+))
function clean_htmlentities ($str) {
return str_replace(array('&','&'),'&',htmlentities($str));
}

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question