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
Solved

REGEX: Convert & to & only in double entities

Posted on 2008-10-24
6
970 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 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