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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jencarnac
Forum Newbie
Posts: 2
Joined: Wed Jan 07, 2004 2:21 pm

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

Post 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);
?>
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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);

?>
Post Reply