2

I have a question that can I replace a certain character like # with # in a string. I have all the character checkers and their replacer in an array. Like this--

$string_check = array(
                      "#" => "#",
                      .... and so on (list is too big)
                     );

So how can I do this thing. Please help me out. I only have 20 days of experience with php.

3 Answers 3

4

You can feed your translation table right into strtr():

$table = array(
    '#' => '...',
);
$result = strtr($source, $table);
Sign up to request clarification or add additional context in comments.

Comments

2

str_replace does exactly that and it also accepts arrays as replacement maps:

$string_check = array(
    "#" => "#"
);

$result = str_replace (array_keys($string_check), array_values($string_check), $original);

Comments

0
$search = array('hello','foo');
$replace = array('world','bar');

$text = 'hello foo';
$result = str_replace($search,$replace,$text);
// $result will be 'world bar'

but in your case it looks like some kind of encoding, have you tried the htmlspecialchars?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.