Solved

REGEX: Convert & to & only in double entities

Posted on 2008-10-24
6
968 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
  • 3
  • 2
6 Comments
 
LVL 27

Expert Comment

by:yodercm
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:yodercm
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 27

Expert Comment

by:yodercm
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

810 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