How can I increase the performance of the following code:
$text = str_replace("A", "B", $text);
$text = str_replace("f", "F", $text);
$text = str_replace("c", "S", $text);
$text = str_replace("4", "G", $text);
//more str_replace here
Do it as one function call:
$text = str_replace(["A","f","c","4"], ["B","F","S","G"], $text);
Related
This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Deprecated: Function eregi_replace() [duplicate]
(2 answers)
Closed 4 years ago.
How can I adjust my code by replacing the "eregi_replace" function with another one that does the same thing?
I know the alternatives, but I do not know how to update the code. O erro que recebo é:
PHP Deprecated: Function eregi_replace
function url($String){
$Separador = "-";
$String = trim($String);
$String = strtolower($String);
$String = strip_tags($String);
$String = eregi_replace("[[:space:]]", $Separador, $String);
$String = eregi_replace("[çÇ]", "c", $String);
$String = eregi_replace("[áÁäÄàÀãÃâÂ]", "a", $String);
$String = eregi_replace("[éÉëËèÈêÊ]", "e", $String);
$String = eregi_replace("[íÍïÏìÌîÎ]", "i", $String);
$String = eregi_replace("[óÓöÖòÒõÕôÔ]", "o", $String);
$String = eregi_replace("[úÚüÜùÙûÛ]", "u", $String);
$String = eregi_replace("(\()|(\))", $Separador, $String);
$String = eregi_replace("(\/)|(\\\)", $Separador, $String);
$String = eregi_replace("(\[)|(\])", $Separador, $String);
$String = eregi_replace("[#®#\$%&\*\+=\|º]", $Separador, $String);
$String = eregi_replace("[;:'\"<>,\.?!_]", $Separador, $String);
$String = eregi_replace("[“”]", $Separador, $String);
$String = eregi_replace("(ª)+", $Separador, $String);
$String = eregi_replace("[´~^°]", $Separador, $String);
$String = eregi_replace("($Separador)+", $Separador, $String);
$String = substr($String, 0, 100);
$String = eregi_replace("(^($Separador)+)|(($Separador)+$)", "", $String);
$String = str_replace("-", $Separador, $String);
return $String;
}
I try to generate a slug from a string, but I got some problems with the german umlauts:
$text = 'Ein schöner Text';
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
$text = trim($text, '-');
$text = iconv('utf-8', 'ASCII//TRANSLIT', $text);
$text = strtolower($text);
$text = preg_replace('~[^-\w]+~', '', $text);
The result should be: 'ein-schoener-text'
Change the second preg_replace line to the below because to match any letter in any language you need to use \p{L} pattern.
$text = preg_replace('~[^\p{L}\d]+~u', '-', $text);
Code:
<?php
$text = 'Ein schöner Text';
$text = preg_replace('~[^\p{L}\d]+~u', '-', $text);
$text = trim($text, '-');
$text = iconv('utf-8', 'ASCII//TRANSLIT', $text);
$text = strtolower($text);
$text = preg_replace('~[^-\w]+~', '', $text);
echo $text;
?>
Output:
ein-schoner-text
I am trying to drop the last vowel in a string. For example:
$string = 'This is a string of words.';
$vowels = array('a','e','i','o','u');
if (in_array($string, $vowels)) {
// $newstring = '' // Drop last vowel.
}
echo $newstring; // Should echo 'This is a string of wrds.';
How can I do this?
Thanks
With a regular expression we could do it:
$str = 'This is a string of words.';
echo preg_replace('/([aeiou]{1})([^aeiou]*)$/i', '$2', $str);
//output: This is a string of wrds.
Explaining a bit more the regular expression:
$ <- the end of the phrase
([aeiou]{1}) <- looks for one vowel
([^aeiou]*) looks for any that is not a vowel
Hope this works
$string = 'This is a string of words.';
$words = explode(" ", $string);
$lastword = array_pop($words);
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U", " ");
$newlastword = str_replace($vowels, "", $lastword);
$newstring='';
foreach ($words as $value) {
$newstring=$newstring.' '.$value;
}
$newstring=$newstring.' '.$newlastword;
echo $newstring;
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 need to check the incoming string and leave only characters, matching:
small case a-z letters
_ character
any numbers
only one dot (first one)
$string = 'contDADdas7.6.asdASDj_##e1!Ddd__aa#S.txt';
$pattern = "/[a-z_0-9]+/";
preg_match_all("/[a-z_0-9]+/", $name, $result);
echo implode('', $result[0]);
has to be
contdas7.6asdj_e1dd__aatxt
It matches first three points, how can I take only one first dot ?
You can try this:
$string = strrev($string);
$string = preg_replace('~[^a-z0-9_.]++|\.(?![^.]*$)~', '', $string);
$string = strrev($string);
An other way:
$strs = explode('.', $string);
if (count($strs)>1) {
$strs[0] .= '.' . $strs[1];
unset($strs[1]);
}
$string = preg_replace('~[^a-z0-9_.]++~', '', implode('', $strs));
<?php
$str = "contDADdas7.6.asdASDj_##e1!Ddd__aa#S.txt";
preg_match_all("/[a-z_0-9\.]+/", $str, $match);
$newstr = implode("", $match[0]);
echo substr_replace(str_replace(".", "", $newstr), ".", strpos($newstr, "."), 0);
Output:
contdas7.6asdj_e1dd__aatxt