Page 1 of 1

replace special characters \\ + * ? [ ^ ] $ ( ) { } = ! <

Posted: Wed Jan 07, 2004 2:21 pm
by jencarnac
Does anyone have a working code that will replace special characters:

\\ + * ? [ ^ ] $ ( ) { } = ! < > | :




I have this so far, but it doesn't work for all chareacters

<?
$patterns = array("/`/", "/~/", "/!/", "/@/", "/#/", "/$/", "/%/", "/^/", "/&/", "/{/", "/}/", "/|/");

$filename = preg_replace($patterns, '', $string);
?>

Posted: Wed Jan 07, 2004 2:55 pm
by redmonkey
As there is no need for any sort of complex pattern matching, str_replace() should be quicker.

Code: Select all

<?php

$patterns = array('/', '\'', '(', ')', ':', '[', ']', '`', '~', '!', '@', '#', '$', '%', '^', '&', '{', '}', '|', '+', '*', '?'); 

$filename = str_replace($patterns, '', $string);

?>