converting letters to special letters - php

i want to make function to change letters to special letters like
i made this code
$text = "hello my name is karl";
$my_array = array('н̈̈','σ̈̈','м̈̈','ӵ̈','ӥ̈','ɑ̈̈','ǝ̈̈','ı̈̈','ƨ̈̈','к̈̈','ɑ̈̈','я','l̈̈');
for ($i = 0, $len = strlen($text); $i < $len; $i++) {
$random = #array_rand($text[$i]); # one random array element number
$get_it = $my_array[$random]; # get the letter from the array
echo $get_it;
}
it should be ( н̈̈ǝ̈̈l̈̈l̈̈σ̈̈ м̈̈ӵ̈ ӥ̈ɑ̈̈м̈̈ǝ̈̈ ı̈̈ƨ̈̈ к̈̈ɑ̈̈я̈̈l̈̈ ) after i print.
the above code not working. so please help to me correct it.
regards

http://php.net/manual/en/function.str-replace.php
Look at this function.
$s = array('e', 'something else to search for');
$r = array('é', 'something to replace "something else to search for" with');
$stringy = str_replace($s, $r, $string);
PHP has functions for everything :P
Edit:
$search = array('h', 'a', 'm', 'e??', 'n', 'a', 'e?', 'i', 'z', 'k', 'a', 'r', 'l');
$replace = array('н̈̈','σ̈̈','м̈̈','ӵ̈','ӥ̈','ɑ̈̈','ǝ̈̈','ı̈̈','ƨ̈̈','к̈̈','ɑ̈̈','я'̈̈,'l̈̈');
$newString = str_replace($search, $replace, $string);

$string = 'The quick brown fox jumped over the lazy dog.';
$patterns = array();
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements = array();
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo preg_replace($patterns, $replacements, $string);
http://php.net/manual/en/function.preg-replace.php

Using http://php.net/manual/en/function.str-replace.php and an array containing your characters you can easily do this
$yourtext = "hello my name is karl"; //Place your text in this
//Match your characters that you'de like to replace
$after = array('н̈̈','σ̈̈','м̈̈','ӵ̈','ӥ̈','ɑ̈̈','ǝ̈̈','ı̈̈','ƨ̈̈','к̈̈','я','l̈̈');
$before = array('h','o','m','y','n','a','e','i','s','k','r','l');
$yourtext = str_replace ( $before , $after , $yourtext, $replacing);
echo $yourtext; //prints н̈̈ǝ̈̈l̈̈l̈̈σ̈̈ м̈̈ӵ̈ ӥ̈ɑ̈̈м̈̈ǝ̈̈ ı̈̈ƨ̈̈ к̈̈ɑ̈̈я̈̈l̈̈

Related

PHP - replace text without overwriting already replaced text

I want to replace specific characters in string without replacing them again. Here is what I am talking about:
// Outputs F because A is replaced with B, then B is replaced with C, and so on...
// Finally E is replaced with F, because of left to right replacements.
$search = array('A', 'B', 'C', 'D', 'E');
$replace = array('B', 'C', 'D', 'E', 'F');
$subject = 'A';
echo str_replace($search, $replace, $subject);
// Outputs: apearpearle pear
// For the same reason mentioned above
$letters = array('a', 'p');
$fruit = array('apple', 'pear');
$text = 'a p';
$output = str_replace($letters, $fruit, $text);
echo $output;
How can I solve this issue?
Sample Code to replace text using strtr function as suggested by #NiettheDarkAbsol
<?php
$replacepair = array('A' => 'B', 'B' =>'C', 'C'=>'D', 'D'=>'E', 'E'=> 'F');
$subject = 'A';
echo strtr($subject, $replacepair); // outputs B
First replace them with some characters which are not in the text. Later replace them it to what you want.
$letters = array('a', 'p');
$fruit = array('*', '#');
$text = 'a p';
$output = str_replace($letters, $fruit, $text);
$letters = array('*', '#');
$fruit = array('apple', 'pear');
$output = str_replace($letters, $fruit, $output);
echo $output;

converting [b] to <b>

I don't have much experience at all with patterns and preg_replace so I know I'm doing it wrong. But I'm not sure what the proper syntax should be.
In this instance I'm trying to convert [/b] to from a form submission.
Here's my best attempt, which obviously is incorrect:
$patterns = array();
$patterns[0] = '/[b]/';
$patterns[1] = '/[/b]/';
$patterns[2] = '/[i]/';
$patterns[3] = '/[/i]/';
$patterns[4] = '/[u]/';
$patterns[5] = '/[/u]/';
$replace = array();
$replace[0] = '/<b>/';
$replace[1] = '/</b>/';
$replace[2] = '/<i>/';
$replace[3] = '/</i>/';
$replace[4] = '/<u>/';
$replace[5] = '/</u>/';
$new = preg_replace($patterns, $replace, $string);
This might be done more simply using str_replace()
$search = array('[b]', '[/b]', '[i]', '[/i]');
$replace = array('<b>', '</b>', '<i>', '</i>');
str_replace($search, $replace, $string );
Replace doesn't need delimiters.
This doesn't need to be a regex, you could do this with str_replace.
The [ and ] are special charachters in regex so they need
to be escaped.
Anywhere you use a delimiter as the character in the regex it needs to be escaped.
All these solutions below are case sensitive. If you allow for capitals as well the i modifier should be used for the regex and the str_ireplace should be used in place of str_replace.
Example 1:
$patterns = array();
$patterns[0] = '/\[b\]/';
$patterns[1] = '/\[\/b\]/';
$patterns[2] = '/\[i\]/';
$patterns[3] = '/\[\/i\]/';
$patterns[4] = '/\[u\]/';
$patterns[5] = '/\[\/u\]/';
$replace = array();
$replace[0] = '<b>';
$replace[1] = '</b>';
$replace[2] = '<i>';
$replace[3] = '</i>';
$replace[4] = '<u>';
$replace[5] = '</u>';
$new = preg_replace($patterns, $replace, $string);
If you want it with a regex though use the regex functionality.
$patterns = array();
$patterns[0] = '/\[(\/?)b\]/';
$patterns[1] = '/\[(\/?)i\]/';
$patterns[2] = '/\[(\/?)u\]/';
$replace = array();
$replace[0] = '<$1b>';
$replace[1] = '<$1i>';
$replace[2] = '<$1u>';
$new = preg_replace($patterns, $replace, $string);
or with str_replace...
$patterns = array();
$patterns[0] = '[b]';
$patterns[1] = '[/b]';
$patterns[2] = '[i]';
$patterns[3] = '[/i]';
$patterns[4] = '[u]';
$patterns[5] = '[/u]';
$replace = array();
$replace[0] = '<b>';
$replace[1] = '</b>';
$replace[2] = '<i>';
$replace[3] = '</i>';
$replace[4] = '<u>';
$replace[5] = '</u>';
$new = str_replace($patterns, $replace, $string);
A better regex...
$new = preg_replace('~\[(/?)(b|i|u)\]~', '<$1$2>', $string);

How to precisely replace the string

$string = 'I am chan, my son is chan_junior';
$search = array('chan', 'chan_junior');
$replace = array('a', 'b');
$new = str_replace($search, $replace, $string);
echo $new;
I want to replace the string to I am a, my son is b, but the result is I am a, my son is a_junior. Is there a function to make it happen?
That happens because php search first for "chan", replace it on 2 places and then don't find second string. Search for longer strings first.
This works:
$string = 'I am chan, my son is chan_junior';
$search = array('chan', 'chan_junior');
$replace = array('a', 'b');
$str_arr = explode(' ', $string);
$count = count($str_arr);
for ($i = 0; $i < $count; $i ++)
{
$word = preg_replace('/^[^A-Za-z0-9\-]*|[^A-Za-z0-9\-]*$/', '', $str_arr[$i]);
if (in_array($word, $search))
{
$key = array_search($word, $search);
$str_arr[$i] = str_replace($word, $replace[$key], $str_arr[$i]);
}
}
$new = implode(' ', $str_arr);
echo $new;

Remove word from a string

I have a csv file that contains company names. I would want to match it against my database. In order to have a cleaner and nearer matches, I am thinking of eliminating some company suffixes like 'inc', ' inc', ', inc.' or ', inc'. Here's my sample code:
$string = 'Inc Incorporated inc.';
$wordlist = array("Inc","inc."," Inc.",", Inc.",", Inc"," Inc");
foreach ($wordlist as &$word) {
$word = '/\b' . preg_quote($word, '/') . '\b/';
}
$string = preg_replace($wordlist, '', $string);
$foo = preg_replace('/\s+/', ' ', $string);
echo $foo;
My problem here is that the 'inc.' doesn't get removed. I'm guessing it has something to do with the preq_quote. But I just can't figure out how to solve this.
Try this :
$string = 'Inc incorporated inc.';
$wordlist = array("Inc","inc.");
foreach ($wordlist as $word) {
$string =str_replace($word, '', $string);
}
echo $string;
OR
$string = 'Inc Incorporated inc.';
$wordlist = array("Inc","inc.");
$string = str_replace($wordlist, '', $string);
echo $string;
This will output as 'corporated'...
If you want "Incorporated" as result, make the "I" is small.. and than run my above code (first one)...
Try this. It may involve type juggling at some point, but will have your desired result
$string = 'Inc Incorporated inc.';
$wordlist = array('Inc', 'inc.');
$string_array = explode(' ', $string);
foreach($string_array as $k => $a) {
foreach($wordlist as $b) {
if($b == $a){
unset($string_array[$k]);
}
}
$string_array = implode('', $string_array);
You can use this
$string = 'Inc Incorporated inc.';
$wordlist = array("Inc "," inc.");
$foo = str_replace($wordlist, '', $string);
echo $foo;
Run this code here
This will work for any number of elements in array...
$string = 'Inc Incorporated inc.';
$wordlist = array("Inc");
foreach($wordlist as $stripped)
$string = preg_replace("/\b". preg_quote($stripped,'/') ."(\.|\b)/i", " ", $string) ;
$foo = preg_replace('/\s+/', ' ', $string);
echo $foo;

preg_replace with overlapping parameters

Im trying to right a script that divides vowels and suffixes with a hyphen.
<?php
$string = 'celebrationing';
$patterns = array();
$patterns[0] = '/a/';
$patterns[1] = '/e/';
$patterns[2] = '/i/';
$patterns[3] = '/o/';
$patterns[4] = '/u/';
$patterns[5] = '/tion/';
$replacements = array();
$replacements[0] = '-a';
$replacements[1] = '-e';
$replacements[2] = '-i';
$replacements[3] = '-o';
$replacements[4] = '-u';
$replacements[5] = '-tion';
echo preg_replace($patterns, $replacements, $string);
?>
The script works fine for vowels, but when i comes to the suffix -tion, it isnt printing the hyphen.
Output:
c-el-ebr-at-i-on-ing
What im thinking is the fact that both -i and -o are vowels is whats messing the whole process up.
How do I allow all six patterns to be recognized, with all instances of -tion superceding -i and -o?
You can use a simpler regex:
preg_replace('/tion|[aeiou]/', "-$0", $string);
regex101 demo
The regex first tries to match tion which thus prevents the matching of i and o later on in the same match.
Not as elegant as Jerry's solution, this one uses negative-lookahead & lookbehind:
<?php
$string = 'celebrationing';
$patterns = array();
$patterns[0] = '/a/';
$patterns[1] = '/e/';
$patterns[2] = '/i(?!on)/';
$patterns[3] = '/o(?<!ti)(?!n)/';
$patterns[4] = '/u/';
$patterns[5] = '/tion/';
$replacements = array();
$replacements[0] = '-a';
$replacements[1] = '-e';
$replacements[2] = '-i';
$replacements[3] = '-o';
$replacements[4] = '-u';
$replacements[5] = '-tion';
echo preg_replace($patterns, $replacements, $string);
OUPUT:
c-el-ebr-a-tion-ing

Categories