0

I've looked at other questions on this similar topic, and tried those suggestions but they don't seem to work.

This is my code:

<?php

$badChars = array('/</', '/>/', '/$/', '/\\/', '/=/', '/@/', '/\//');

$cleanData = "Text -with /stuff I don@'t want";

echo $cleanData . "\n";

$cleanData = preg_replace($badChars, '', $cleanData);

echo $cleanData . "\n";

?>

Note that the array of patterns will vary based on the scenario. This is for a data cleansing exercise. e.g.: if processing an email field, we'd temporarily drop the @ pattern.

And this is the output:

Text -with /stuff I don@'t want

PHP Warning:  preg_replace(): No ending delimiter '/' found in /home/tim/xm_code/symphony/code/components/controllers/test.php on line 9

Process finished with exit code 0

I can't find anything to help me resolve this. Any ideas?

2
  • @chris85 - forgot to say i need to be able to remove or add elements from the array of patterns depending on the scenario. Commented Oct 17, 2016 at 3:05
  • Still should be able to use the str_replace unless you are doing something more advanced then replacing single characters. e.g. you'll have the same replace array just remove/add whatever you want to it. Commented Oct 17, 2016 at 3:08

1 Answer 1

1

Your double backslash is escaping your closing delimiter. So

'/\\/'

needs to be

'/\\\/'

Alternatively use a character class and you don't need the array. Second alternative, use str_replace since the characters are all static.

$badChars = array('<', '>', '$', '\\', '=', '@', '/');
$cleanData = "Text -with /stuff I don@'t want";
echo $cleanData . "\n";
$cleanData = str_replace($badChars, '', $cleanData);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.