Skip to content
Advertisement

Own Encoding System with str_replace

I was thinking about creating a own encode system with just using str_replace. But is this even possible or should I use other methods?

I had this

$provided_data = $_POST["provided_data"];

$pd2 = str_replace("a", "[...]", $provided_data);
$pd3 = str_replace("b", "[...]", $pd2);
$pd4 = str_replace("c", "[...]", $pd3);
[shortend]
$pd28 = str_replace(" ", "[...]", $pd27);

$encrypted_data = $pd28;

The result should show the encrypted data. But when I try it, it doesn’t return me anything.

Advertisement

Answer

Make sure the input is passed correctly. The below code prints “[…][…][…][…][…]”.

<?php
$provided_data = 'hello';

$pd2 = str_replace("h", "[...]", $provided_data);
$pd3 = str_replace("e", "[...]", $pd2);
$pd4 = str_replace("l", "[...]", $pd3);
$pd28 = str_replace("o", "[...]", $pd4);

echo $pd28; //prints "[...][...][...][...][...]"

?>

Online PHPFiddle: http://phpfiddle.org/

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement