Jump to content

[SOLVED] Case Insensitivity


marcus

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;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.