Jump to content

Recommended Posts

How can I use preg_replace to replace any of the "bad values," in my "bad values" array, and use case insensitivity.

 

So: THIS = thIs.

 

<?php
$js_filter_array_bad = array(
		'onmouseout', 'onmouseover',
		'onclick', 'ondblclick',
		'onkeydown', 'onkeypress',
		'onkeyup', 'onmousemove',
		'onmouseup', 'onmousedown'
		);
$replace = "";
$string = "onMouseOut";
echo preg_replace("|$js_filter_array_bad|iU",$replace,$string);
?>

Link to comment
https://forums.phpfreaks.com/topic/66085-solved-case-insensitivity/
Share on other sites

How's this method?

 

<?php
$filter = array('onmouseout', 'onmouseover',
	'onclick', 'ondblclick',
	'onkeydown', 'onkeypress',
	'onkeyup', 'onmousemove',
	'onmouseup', 'onmousedown'
	);
$replace = "";
$string = "onMouseOut";
for ($i=0; $i < sizeof($filter); $i++)
if (strtolower($string) == $filter[$i])
	$string = $replace;
?>

Yeah, 'i' is the ticket.

 

<?php
// The "i" after the pattern delimiter indicates a case-insensitive search
if (preg_match ("/php/i", "PHP is the web scripting language of choice.")) {
   print "A match was found.";
} else {
   print "A match was not found.";
}
?>

Ken your method works great if the string is alone by itself, but how would I go about checking the whole string to see if any of the "bad-script" filters are in the string and only replace the "bad-script" words.

 

<?php
$filter = array('onmouseout', 'onmouseover',
	'onclick', 'ondblclick',
	'onkeydown', 'onkeypress',
	'onkeyup', 'onmousemove',
	'onmouseup', 'onmousedown'
	);
$replace = "";
$string = "I like using onmouseUp";
echo preg_replace("/$filter/i",$replace,$string);
?>

try defining the regex within your filter array variable instead:

<?php
$filter = array( '/onmouseout/i', '/onmouseover/i',
                 '/onclick/i', '/ondblclick/i',
                 '/onkeydown/i', '/onkeypress/i',
                 '/onkeyup/i', '/onmousemove/i',
                 '/onmouseup/i', '/onmousedown/i');

$replace = "";
$string = "I like using onmOUseDown";

echo preg_replace($filter, $replace, $string);
?>

 

When you do this:

"/$filter/i"

 

You are passing the following to the preg_replace:

"/Array/i"

 

$filter is an array, but you surround $filter within quotes and pad '/' and '/i' around the variable. Now you cannot join arrays to strings and so PHP cannot do the replacements you are asking it.

 

If you define the regex within the filter array. PHP will process the array and do the replacements. As PHP will recognise that you have passed an array and so will loop through the array passing each item from the array into preg_replace.

 

EDIT

Hold on... I've gone mad :). Why not just do the following instead:

<?php
$filters = array( 'onmouseout', 'onmouseover',
                 'onclick', 'ondblclick',
                 'onkeydown', 'onkeypress',
                 'onkeyup', 'onmousemove',
                 'onmouseup', 'onmousedown'
);

$replace = null;
$new_string = null;
$string = 'I like using onmOUseDown  onKeyup';

// loop through the filters.
foreach($filters as $filter)
{
    $string = preg_replace("/$filter/i", $replace, $string);
}

echo $string;
?>

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.