marcus Posted August 22, 2007 Share Posted August 22, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/66085-solved-case-insensitivity/ Share on other sites More sharing options...
Fadion Posted August 22, 2007 Share Posted August 22, 2007 I suck at regex but shouldnt just the 'i' make the search case insensitive? Quote Link to comment https://forums.phpfreaks.com/topic/66085-solved-case-insensitivity/#findComment-330512 Share on other sites More sharing options...
Ken2k7 Posted August 22, 2007 Share Posted August 22, 2007 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/66085-solved-case-insensitivity/#findComment-330517 Share on other sites More sharing options...
dbillings Posted August 22, 2007 Share Posted August 22, 2007 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."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/66085-solved-case-insensitivity/#findComment-330518 Share on other sites More sharing options...
keeB Posted August 22, 2007 Share Posted August 22, 2007 also, you can use this function: http://php.net/eregi Quote Link to comment https://forums.phpfreaks.com/topic/66085-solved-case-insensitivity/#findComment-330561 Share on other sites More sharing options...
marcus Posted August 22, 2007 Author Share Posted August 22, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/66085-solved-case-insensitivity/#findComment-331205 Share on other sites More sharing options...
wildteen88 Posted August 22, 2007 Share Posted August 22, 2007 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/66085-solved-case-insensitivity/#findComment-331224 Share on other sites More sharing options...
marcus Posted August 22, 2007 Author Share Posted August 22, 2007 Hotness prevails, works like a charm Quote Link to comment https://forums.phpfreaks.com/topic/66085-solved-case-insensitivity/#findComment-331282 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.