How To Generate The Same Sentence Unique Every Time Using PHP And Latin Character

Hello good peoples, today I am going to show you how to generate same sentence unique each time action using PHP. Maybe, you already understand by seeing How to generate the same sentence unique every time using PHP and Latin character title. First, the question is why you should generate a unique sentence every time. The answer is I have developed SMS Server Software for my client to manage his business. He manages his customer's question and answers via this application by using logic like dialog flow. I have developed the logic system. He faced a problem that the sending provider and mobile operators blocks same message sending every time. He requested me to solve the problem so that I have developed the system that the system sends the same message every time with a unique style. For developing the system I had to use Latin characters. The Latin characters are used instead of the normal alphabet. So that I was collected the list of the Latin alphabet and process with my system. Let's go to the main point, our code.

First I have created a function for each alphabet replace to Latin. The function name is shh_latin_get() .This function will find the Latin alphabet of any character like a, b, c, d. Let's see the code of the function.

<?php

function shh_latin_get($char){

if(!ctype_alpha($char)){ 

return $char;

}

if(!ctype_lower($char)){ 

return $char;

}

$a = ['ā','ą','a̧','ą̊'];

$b = ['ɓ'];

$c = ['ç'];

$d = ['đ','ɗ','ɖ'];

$e = ['ę','ȩ'];

$f = ['ƒ'];

$g = ['ǥ','ɠ'];

$h = ['ħ','ɦ'];

$i = ['į','i̧','ɨ','ɨ̧'];

$j = ['j'];

$k = ['ƙ'];

$l = ['ł'];

$m = ['m̧','m̂','m̄'];

$n = ['ɲ','ʼn','ń','n̂','ṅ','n̈','ň','n̄','ñ','ņ','ṋ'];

$o = ['ó','ò','ô','ȯ','ȱ','ö','ȫ','ǒ','ŏ','ō','õ','ȭ','ő','ọ','ơ'];

$p = ['p̄','p'];

$q = ['ǫ','o̧','ø','ǿ'];

$r = ['ɍ','ŕ','ř','ŗ'];

$s = ['ś','ŝ','ṡ','š','ş','ṣ','ş'];

$t = ['ť','ț','ṭ','ṱ','ţ','ŧ'];

$u = ['ų','u̧','ư','ʉ','ú','ù','û','ü','ǔ','ŭ','ū','ũ','ű','ů','ụ'];

$v = ['v'];

$w = ['ẃ','ẁ','ŵ','ẅ'];

$x = ['x'];

$y = ['y̨','ƴ','ý','ỳ','ŷ','ÿ','ȳ','ỹ'];

$z = ['ź','ż','ž','ẓ'];

shuffle($$char);

$character = $$char;

return $character[0];

}

?>


This function will return a Latin character for every normal character. Like for the character the result will be like ā or ą or a̧ or ą̊. The result will be selected each time randomly by the built-in function 
shuffle() randomly because I stored a's multiple Latin characters in $a variable as an array. And the shuffle() function helps us to randomize the position of the character in the array. I've choosed the zero position using $character[0] and returned it. 

Now it's time to work with a full sentence. For that, I have to create another function to reduce code complexity. I have created a new function name as shh_convert_latin() and this function will help us to process convert system for a full sentence. Let's write the code and after this, I will explain it.

<?php

function shh_convert_latin($s){

global$number_of_change_word;

$explode_message = explode(" ",$s);

$new_message = '';

foreach($explode_messageas $word) {

$word_len = strlen($word);

$rand_char = rand(0,$word_len-1); 

$target_char = $word[$rand_char]; 

$replaced_char = shh_latin_get($target_char);      

$word = str_replace($target_char,$replaced_char,$word);     

$new_message.=$word. " ";

}       

$new_message = trim($new_message);

return $new_message;

}

?>


In this function, I explode every word of a sentence and chose a character randomly from the word and replace it with our previous build shh_latin_get()  function and then combine the sentence again. If you see the code you will clear it. Now we will choose our sentence and run it with the function. Suppose we choose a sentence,

My momma always said, “Life was like a box of chocolates. You never know what you're gonna get.”

Now, will run this sentence with the function we have created. Let's see the first time result.

 

Number - 1

Mý mommā alŵays sai̧d, “Lįfe waş łike ā bôx oƒ çhoçolates. Yȱu nȩvȩr knoŵ wha̧t yoų're gonną̊ gȩt.”



Number - 2

My m̧om̧m̧a ałways saɨ̧d, “Lifę waś łike ą bōx òf chocolatȩs. Yôu neveɍ knǒw wħat you'rȩ goṅṅa get.”



Number - 3

Mý mõmma alwayš sai̧d, “Life wās łike ą ɓox óf chȭcȭlates. Yoů nęvęr knoẅ ẅhat yoû're gonną̊ get.”



Number - 4

My m̧om̧m̧a alwayṣ saiđ, “Lɨ̧fe waş łike a̧ box ơf chocołates. Yoű nȩvȩr knoẅ ŵhat you'rę ɠonna ɠet.”



Number - 5

Mỳ mommā ālwāys ṣaid, “Life waṡ łike ą box ȯf çhoçolates. Yoů never knŏw wɦat you'rę goñña get.”


See, the system created new sentences each time in my 5 results. The sentence is the same but words are replaced with special characters. So that any system will count each system as a new sentence. We have created unique sentences successfully.  Though the code is a little bit of complicated but the system is awesome and helpful. I hope you understand this if you have any question on this you can ask me directly via comment. Have a good time, Happy coding. Bye!

Sajjad Hossain
Hi, I'm Sajjad Hossain, a PHP Developer working on PHP Development since 2012. If you have a project that you want to start you can contact me. I'm currently available for freelance work. Ping me on Facebook, Messenger, WhatsApp or Email.

We use cookies on our website. To find out more about how and why they are used or opt-out, please read our Cookie Policy. By choosing "I Accept", you consent to our use of cookies. Cookie Policy
Top