I have written this method to replace special characters:
function replace_sonder($string)
{
$string2 = str_replace("ä", "ä", $string);
$string2 = str_replace("%E4", "ä", $string2);
$string2 = str_replace("ö", "ö", $string2);
$string2 = str_replace("%F6", "ö", $string2);
$string2 = str_replace("ü", "ü", $string2);
$string2 = str_replace("%FC", "ü", $string2);
$string2 = str_replace("Ä", "Ä", $string2);
$string2 = str_replace("%C4", "Ä", $string2);
$string2 = str_replace("Ö", "Ö", $string2);
$string2 = str_replace("%D6", "Ö", $string2);
$string2 = str_replace("Ü", "Ü", $string2);
$string2 = str_replace("%DC", "Ü", $string2);
$string2 = str_replace("ß", "ß", $string2);
$string2 = str_replace("%DF", "ß", $string2);
return $string2;
}
it always returns the same string that I pass in. Where am I missing something or is there an alternative way to do this?
$string = preg_replace("/ä/", "ä", $string);
...
but better way is:
$string = htmlentities($string, ENT_QUOTES);
Check the output you're comparing is not to an HTML page as it will convert the characters back again.
Related
How can I replace "/" and " " from the given string "one size x/l" and generate output like "one-size-x-l" using preg_replace.
Using Regular expression you can replace all the special characters by "-" as below:
$str= "one size x/l";
$str= preg_replace("![^a-z0-9]+!i", "-", $str);
Hope this helps:)
Use the below function, which will make fnie urls as required
function seoUrlAscii($str, $replace=array(), $delimiter='-') {
if( !empty($replace) ) { $str = str_replace((array)$replace, ' ', $str); }
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
return $clean;
}
$a="one size x/l";
echo preg_replace('(/|\s)','-', $a);
got my output as one-size-x-l
is it possible to make this more smooth with less line of codes since i have to repeat it for every new box i need to insert it into.
$fil_namn = str_replace("5FSE_", "", $fil_url);
$fil_namn = str_replace(".pdf", "", $fil_namn);
$fil_namn = str_replace(".docx", "", $fil_namn);
$fil_namn = str_replace(".doc", "", $fil_namn);
$fil_namn = preg_replace("[_]",". ",$fil_namn);
$fil_namn = preg_replace('/^[0-9]+\. +/','', $fil_namn);
$fil_namn = preg_replace ("[AaA]","å",$fil_namn);
$fil_namn = preg_replace ("[AeA]","ä",$fil_namn);
$fil_namn = preg_replace ("[OoO]","ö",$fil_namn);
$fil_namn = preg_replace ("[aAa]","Å",$fil_namn);
$fil_namn = preg_replace ("[aEa]","Ä",$fil_namn);
$fil_namn = preg_replace ("[oOo]","ö",$fil_namn);
$fil_namn= str_replace("."," ", $fil_namn);
You could use this:
str_replace(array('5FSE_', '.pdf', '.docx', '.doc'), '', $fill_namn);
str_replace allows for arrays.
You can also do this:
$string = "Hello";
echo str_replace(array("H", "e", "l", "o"), array("A", "l", "e", "x"), $string);
This will print out Aeeex.
Another method would be to use the strtr() function:
$string = "[AaA][AeA][OoO][aAa][aEa][oOo]";
$find = array("[AaA]", "[AeA]", "[OoO]", "[aAa]", "[aEa]", "[oOo]");
$replace = array("å", "ä", "ö", "Å", "Ä", "ö");
echo strtr($string, array_combine($find, $replace));
This echoes out:
åäöÅÄö
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̈̈
I have a string like this one text more text "empty space".
How can I replace the space in "empty space" and only this space with ###?
$string = 'text more text "empty space"';
$search = 'empty space';
str_replace($search, 'empty###space', $string);
How about this, with no regular expressions:
$text = 'foo bar "baz quux"';
$parts = explode('"', $text);
$inQuote = false;
foreach ($parts as &$part) {
if ($inQuote) { $part = str_replace(' ', '###', $part); }
$inQuote = !$inQuote;
}
$parsed = implode('"', $parts);
echo $parsed;
$somevar = "empty space";
$pattern = "/\s/";
$replacement = "###";
$somevar2 = preg_replace($pattern, $replacement, $somevar);
echo $somevar2;
$string = "My String is great";
$replace = " ";
$replace_with = "###";
$new_string = str_replace($replace, $replace_with, $string);
This should do it for you. http://www.php.net/manual/en/function.str-replace.php
Edited after you comments
Maybe it's not the best solution, but you can do it like this:
$string = 'text more text "empty space"';
preg_match('/(.*)(".*?")$/', $string, $matches);
$finaltext = $matches[1] . str_replace(' ', '###', $matches[2]);
I get data from the database that is utf8 encoded. But somehow some old data contains latin1 characters.
So this
$encod = mb_detect_encoding($string, 'UTF-8', true);
always is correct.
Is it safe to always use utf8_decode() to check for latin1 characters like 'äöüß'???
$string = utf8_decode($string);
$search = Array(" ", "ä", "ö", "ü", "ß", "."); //,"/Ä/","/Ö/","/Ü/");
$replace = Array("-", "ae", "oe", "ue", "ss", "-"); //,"Ae","Oe","Ue");
$string = str_replace($search, $replace, strtolower($string));
Regards
It seems to work without the utf8_encoding:
<?php
$string = "äöüß";
$search = Array(" ", "ä", "ö", "ü", "ß", "."); //,"/Ä/","/Ö/","/Ü/");
$replace = Array("-", "ae", "oe", "ue", "ss", "-"); //,"Ae","Oe","Ue");
$string = str_replace($search, $replace, strtolower($string));
echo $string;
?>
DEMO: http://codepad.org/HGTyHkBU
Use htmlspecialchars(); it is more safer for work.
More info:
http://php.net/manual/en/function.htmlspecialchars.php