Jump to content

slash / \ syntax


dsaba

Recommended Posts

i'm trying to write a script that keeps people from posting any kind of html/javascript/mysql statements

 

so first I take care of people trying to post html or javascript code that does not look like html or javascript

 

I translate anything that might be html into html or javascript with this

$string = html_entity_decode($string);

 

now once everything looks like <script>  I want to remove those tags and other characters

$string = preg_replace($pattern, "", $string);

 

 

I put what I want to remove in an array called $pattern, the problem I have is I know what I want to remove i want to remove: <, >, /, \, ', `, "

-all those characters, i want those to be in my pattern array

 

$pattern[0] = "/</";

$pattern[1] = "/>/";

$pattern[2] = "///";

$pattern[3] = "/\/";

$pattern[4] = "/"/";

$pattern[5] = "\'\";

$pattern[6] = "`";

 

thats what I got so far, but i'm confused as hell as how to write that in the array, with the slashes...

 

here's the full code all together:

$pattern[0] = "/</";
$pattern[1] = "/>/";
$pattern[2] = "///";
$pattern[3] = "/\/";
$pattern[4] = "/"/";
$pattern[5] = "\'\";
$pattern[6] = "`";

$string = "A 'quote' is <b>bold</b&gt";
echo "this is before i filter";
echo $string;
$string = html_entity_decode($string);
$string = preg_replace($pattern, "", $string);
echo "this is after i filter";
echo $string;

 

 

the problem is i get all kinds of errors like:

this is before i filterA 'quote' is <b>bold</b>

Warning: preg_replace(): No ending matching delimiter '>' found in  on line 29

 

Warning: preg_replace(): No ending delimiter '>' found in  on line 29

 

Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in  on line 29

 

Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in  on line 29

 

Warning: preg_replace(): No ending delimiter '"' found  on line 29

 

Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in / on line 29

 

Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in  on line 29

this is after i filterA 'quote' is bold</b>

 

 

so how can I fix my pattern array to take the symbols i listed above out of the string?

 

Link to comment
Share on other sites

i want to array my delimiters because i want to remove all of those characters at the same time

 

i dont see how arraying different patterns would help me?

 

could you tell me how to write that i want all those characters removed in an array called $pattern

 

thats my problem

Link to comment
Share on other sites

If you're only replacing one character at a time, str_replace would be the better method.

 

<pre>
<?php
$string = 'a<b>c/d\\';
$chars = array('<', '>', '/', '\\');
foreach ($chars as $char) {
	echo "<b>Removing '$char'...</b><br>";
	echo $string = preg_replace('/' . preg_quote($char, '/') . '/', '', $string);
	echo '<br>';
}
?> 
</pre>

Link to comment
Share on other sites

ok i understand that code except for the part where

 

you mention $chars as $char

 

nowhere do I see the variable $char mentioned..

 

what does that statement mean?

 

(yes i tried the code out it works, but i need to know why in order to use it)

 

thanks again

Link to comment
Share on other sites

$char is created form the foreach loop. It holds the value of each item in the array. Read up on foreach in order to understand what is going on.

 

It basically holds the value of the item the array pointer is at. So on the first loop $char will hold '<' as the value, the second loop it will hold '>' and so on through out the array.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.