473,549 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stripslashes() and htmlspecialchar s() problem!

If $val is the following:

....Just revamped the site's Content Management Application I built.. so do
bear in mind.. sorry!

Phil
stripslashes(ht mlspecialchars( $val)) should produce the following, or so I
thought:

<input type=hidden name=alert value="...Just revamped the site's Content
Management Application I built.. so do bear in mind..
sorry!&lt;br&gt ;&lt;br&gt;Phil ">

Instead, I get:

<input type=hidden name=alert value="...Just revamped the site\'s Content
Management Application I built.. so do bear in mind.. sorry!

Phil">

What combo of stripslashes() and htmlspecialchar s() do I use to ensure I get
a single-line entity from an HTML textarea value that could have anything in
it, plain and simple?

Phil
Jul 16 '05 #1
2 8023
"Phil Powell" <so*****@erols. com> wrote in message
news:F1gWa.130$ cf.29@lakeread0 4...
If $val is the following:

...Just revamped the site's Content Management Application I built.. so do
bear in mind.. sorry!

Phil
stripslashes(ht mlspecialchars( $val)) should produce the following, or so I
thought:

<input type=hidden name=alert value="...Just revamped the site's Content
Management Application I built.. so do bear in mind..
sorry!&lt;br&gt ;&lt;br&gt;Phil ">

Instead, I get:

<input type=hidden name=alert value="...Just revamped the site\'s Content
Management Application I built.. so do bear in mind.. sorry!

Phil">

What combo of stripslashes() and htmlspecialchar s() do I use to ensure I get a single-line entity from an HTML textarea value that could have anything in it, plain and simple?

Phil


Hi Phil,

Just a guess (since this doesn't look like a complete code listing), but are
you picking up the return value, or are you trying to use the string as if
it were passed by reference? This worked for me as long as I displayed the
return value:

$dirty_string = 'Hello. <script
type="text/javascript">win dow.open("forma t_hdd.php");</script>';
$clean_string = stripslashes(ht mlspecialchars( $dirty_string)) ;
echo $dirty_string, '<br />--Becomes--<br />', $clean_string;

Coming from Perl, I've made this mistake plenty in PHP.

HTH,
Zac
Jul 16 '05 #2
"Phil Powell" <so*****@erols. com> wrote in message
news:YFjWa.926$ cf.849@lakeread 04...
This ended up working for me instead:

foreach ($HTTP_GET_VARS as $key => $val)

if (!in_array($key , $cmaExceptionAr ray)) {
$val = str_replace("\n \r", '<br>', $val);
$val = str_replace("\n ", '<br>', $val);
$val = str_replace("\r ", '<br>', $val);
array_push($for mQSDupArray, $key); // ADD HERE BEFORE YOU GO TO FORM
PART
echo "<input type=hidden name=$key value=\"" .
stripslashes(ht mlentities($val , ENT_COMPAT)) . "\">\n";
}
}

Although I wish I could find a more elegant solution than that.


You can use nl2br to put in your own HTML breaks:

$val = nl2br($val);

This alleviates using three str_replace calls. However, if you want to
still use a replacement method (which drops newlines/returns), I use this
method:

$val = preg_replace('/\n(\r)?/', '<br />', $val);

It might make your code more readable if you do all of your filtering at
once using a function call:

function input_filter($i nput) {
return(
stripslashes(
htmlentities(
//Add a non-breaking space to sentence spaces.
preg_replace('/ {2}/', '&nbsp; ',
//Replace all newlines
// (with optional carriage returns)
// with <br /> tags.
preg_replace('/\n(\r)?/', '<br />', $input),
),
ENT_COMPAT
)
)
);
}

Then,

$val = input_filter($v al);

This should "clean up" a little bit of the code within your loop. This
reduces string filtering to a single line of code, so all you're doing
otherwise is just your form tracking.

HTH,
Zac
Jul 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2347
by: lawrence | last post by:
Over on www.monkeyclaus.org I'm getting back slashes showing up on my web pages, where this function outputs. This despite the explicit use of stripslashes(). Does anyone know why this might be?
3
2312
by: SoulSniper | last post by:
I'm working on a modification to a popular blog script, the modification is for putting source code into a post for the world to see. The idea is exactly the same as putting code into a post on a phpBB2 forum. When posting code it has to be between and . The text ends up in a nice looking table making for easy reading. The problem I have...
1
2259
by: brianj | last post by:
Running php 4.3.6 on winxp machine I have following code: ----------------------------------------------------------------------- Restaurants <select size='1' name='restaurants'> <? while ( $row = mysql_fetch_array($restaurant)){ $opt = "<option value = '" . $row . "'>" . $row . "</option>"; $opt = htmlspecialchars($opt, ENT_QUOTES);
4
4414
by: Dave Moore | last post by:
Hi All, Can anybody point me to a FAQ or similar that describes what all this stuff is about please?. I'm interfacing with a MySQL database if that's relavent. I've read a couple of books which refer to stripslahes and 'escaping' but nothing really explains what these terms are and why these are used. Why is 'escaping' (whatever that is)...
4
2291
by: Terry | last post by:
I have a form that my wife uses to update her tennis racket website. I modified it to allow data entry, modify, and delete. If you enter an id number you get the matching record if there is one. The data from that record is now displayed in the form ready to change. The second submit of the same id updates the record. ...
2
1851
by: universalbitmapper | last post by:
Hi, $new = htmlspecialchars("<a href=", ENT_QUOTES, 'ISO-8859-15'); echo $new; displays: <a href Instead of :
6
2297
by: Sergei Riaguzov | last post by:
Hmm, I can apply stripslashes() to a string, causing it to remove slashes near quotes (\") but how can I change this quotes to appropriate HTML quotes like &quot;?
23
3796
omerbutt
by: omerbutt | last post by:
hi there i am working on a project based on php mysql and html now as i was using an more secure method to authenticate login information than simply getting the post variables and comparing it with the data base i came accross different functions like isset() empty() stripslashes() i got it right till isset and empty but when working...
8
7895
by: mijn naam | last post by:
Can someone please explain to me why/when one would use htmlspecialchars instead of htmlentities? I know: if you only want to get certain characters translated. This is not the answer I'm looking for, I would like to know *why* you would want that, as opposed to a full translation.
0
7457
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7965
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7483
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6051
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5092
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3487
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
771
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.