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;
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;
}
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);
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
This question already has answers here:
Make all words lowercase and the first letter of each word uppercase
(3 answers)
Closed 1 year ago.
This post was edited and submitted for review last month and failed to reopen the post:
Original close reason(s) were not resolved
$q = 'durham-region';
$q = ucfirst($q);
$q = 'Durham-region';
How would I capitalize the letter after the dash (Durham-Region)? Would I have to split the two and capitalize?
Updated Solution
As of PHP 5.5, the e modifier for preg_replace has been deprecated. The best option now is to use one of the more modern suggestions that does not use this, such as:
$q = ucwords($q, '-);
or
$q = implode('-', array_map('ucfirst', explode('-', $q)));
Original Answer
You could use preg_replace using the e modifier this way:
$test = "durham-region";
$test = preg_replace("/(\w+)/e","ucfirst('\\1')", $test);
echo $test;
// Durham-Region
Thanks to the delimiter parameter of ucwords, since PHP 5.4.32 and 5.5.16, it is as simple as this:
$string = ucwords($string, "-");
A one-liner that doesn't envolve using the e PCRE modifier:
$str = implode('-', array_map('ucfirst', explode('-', $str)));
another oneliner:
str_replace(' ','',ucwords(str_replace('-',' ',$action)))
Yes. ucfirst() simply capitalized the first letter of the string. If you want multiple letters capitalized, you must create multiple strings.
$strings = explode("-", $string);
$newString = "";
foreach($strings as $string){
$newString += ucfirst($string);
}
function ucfirst_all($delimiter, $string){
$strings = explode("-", $string);
$newString = "";
foreach($strings as $string){
$newString += ucfirst($string);
}
return $newString;
}
You could do it with a regular expression callback method like this:
$q = preg_replace_callback('/\-([a-z]+)/g', create_function(
'$m', 'return "-" . ucfirst($m[1]);'
),$q)
It is important to note that the solutions provided here will not work with UTF-8 strings!
$str = "Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει-υπέρ νωθρού κυνός";
$str = explode('-', mb_convert_case( $str, MB_CASE_TITLE ) );
$str = implode('-', array_map('mb_convert_case', $str, array(MB_CASE_TITLE, "UTF-8")) );
echo $str;
// str= Τάχιστη Αλώπηξ Βαφήσ Ψημένη Γη, Δρασκελίζει-Υπέρ Νωθρού Κυνόσ
look
function UpperCaseAfterDash($wyraz)
{
$rozbij = explode('-',$wyraz);
echo $rozbij[0].'-'.
ucfirst($rozbij[1]);
}
UpperCaseAfterDash("input-text");
Above function returns input-Text
If you need only uppercase letter after one dash for example with city names (Jastrzębie-Zdrój) it will be enough, but if you need more than one... , just count how many array elements (after explode in above code) exists, then use loop.
Greets,
Here's a generalized function that lets you capitalize after any character or string of characters. In the specific case the questioner asked about, the needle is the dash.
function capitalizeAfter( $needle, $haystack ) {
$haystack = str_replace( $needle . "a", $needle . "A", $haystack );
$haystack = str_replace( $needle . "b", $needle . "B", $haystack );
$haystack = str_replace( $needle . "c", $needle . "C", $haystack );
$haystack = str_replace( $needle . "d", $needle . "D", $haystack );
$haystack = str_replace( $needle . "e", $needle . "E", $haystack );
$haystack = str_replace( $needle . "f", $needle . "F", $haystack );
$haystack = str_replace( $needle . "g", $needle . "G", $haystack );
$haystack = str_replace( $needle . "h", $needle . "H", $haystack );
$haystack = str_replace( $needle . "i", $needle . "I", $haystack );
$haystack = str_replace( $needle . "j", $needle . "J", $haystack );
$haystack = str_replace( $needle . "k", $needle . "K", $haystack );
$haystack = str_replace( $needle . "l", $needle . "L", $haystack );
$haystack = str_replace( $needle . "m", $needle . "M", $haystack );
$haystack = str_replace( $needle . "n", $needle . "N", $haystack );
$haystack = str_replace( $needle . "o", $needle . "O", $haystack );
$haystack = str_replace( $needle . "p", $needle . "P", $haystack );
$haystack = str_replace( $needle . "q", $needle . "Q", $haystack );
$haystack = str_replace( $needle . "r", $needle . "R", $haystack );
$haystack = str_replace( $needle . "s", $needle . "S", $haystack );
$haystack = str_replace( $needle . "t", $needle . "T", $haystack );
$haystack = str_replace( $needle . "u", $needle . "U", $haystack );
$haystack = str_replace( $needle . "v", $needle . "V", $haystack );
$haystack = str_replace( $needle . "w", $needle . "W", $haystack );
$haystack = str_replace( $needle . "x", $needle . "X", $haystack );
$haystack = str_replace( $needle . "y", $needle . "Y", $haystack );
$haystack = str_replace( $needle . "z", $needle . "Z", $haystack );
return $haystack;
}