ucfirst() not working properly with scandinavic characters [duplicate] - php

This question already has answers here:
strtolower() for unicode/multibyte strings
(8 answers)
Closed 1 year ago.
How to get ucfirst() working with scandinavic characters?
$str = "SÄKYLÄ";
echo ucfirst(strtolower($str)); //prints SÄkylÄ
One possibility is use mb_convert_case() but I'd like to know if this is possible using ucfirst()
$str = "SÄKYLÄ";
echo mb_convert_case($str, MB_CASE_TITLE, "UTF-8"); //prints Säkylä
Which function is faster for string capitalization?

Your problem here is not ucfirst() it's strtolower(). You have to use mb_strtolower(), to get your string in lower case, e.g.
echo ucfirst(mb_strtolower($str));
//^^^^^^^^^^^^^^ See here
Also you can find a multibyte version of ucfirst() in the comments from the manual:
Simple multi-bytes ucfirst():
<?php
function my_mb_ucfirst($str) {
$fc = mb_strtoupper(mb_substr($str, 0, 1));
return $fc.mb_substr($str, 1);
}
Code from plemieux from the manual comment

Related

How to add - in-between strings [duplicate]

This question already has answers here:
Insert string at specified position
(11 answers)
Closed 3 years ago.
How do I add - in between strings.
Let’s say for instance, I want to add - in 123456789101 at the fourth position three times thereby making it look like this : 1234-5678-9101.
Substr_replace() or str_replace has not solved the problem.
I would use combination of str_split and implode.
$code = 123456789101;
$formatted = implode('-', str_split($code, 4) );
echo $formatted; //1234-5678-9101
There are a number of ways to do this.
Use substr
$output = sprintf('%s-%s-%s', substr($string, 0,4), substr($string,4,4), substr(8,4));
Use preg_replace
$output = preg_replace('/(.{4,4})(.{4,4})(.{4,4})/', '$1-$2-$3', $string);

use str_replace to remove after # [duplicate]

This question already has answers here:
removing #email.com from string in php
(6 answers)
Closed 4 years ago.
How can I use the str_replace() without using an array of words for turning:
some#mail.com
into
some
So, everything after the '#' sign includes # as well.
How can I do that?
As an example, i will type 'adminofsite#xxxwwweeerandomstuff.com'
and the output will be: 'adminofsite'.
use strstr
$email="john#doe.com"
$user = strstr($email, '#', true);
echo $user;
$str = "some#mail.com"
$str = substr($str,0,strpos($str,"#"))
function stripEmailDomain($string){
return substr($string, 0, strpos($string, '#'));
}
I exploded and then rebuilt the string. This will work if you are positive the string will always be an email address. Its a work around but seems flexible if you want to modify it for a specific purpose.
<?php
$explo0= explode('#',"some#mail.com");
for($i=0;$i<count($explo0);$i++){
$exploresult0.=$explo0[$i+1];
}
echo "#".$exploresult0;
?>

PHP ltrim() not working as expected [duplicate]

This question already has answers here:
PHP ltrim behavior with character list
(2 answers)
Closed 8 years ago.
I have this code..
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = ltrim($homepage1, 'datastring=/mac_project');
echo $trimmed;
I get the output as folio/kitchen/images/03.jpg. It's missing the /port from the /portfolio directory.
Full output should've been /portfolio/kitchen/images/03.jpg
Why not do the simple str_replace() ?
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = str_replace('datastring=/mac_project','',$homepage1);
echo $trimmed;// "prints" /portfolio/kitchen/images/03.jpg
The second parameter for ltrim is for character_mask, which means all the chars in the list will be trimmed.
You could use str_replace(), or if you want to replace only at the beginning of the string by preg_replace():
$trimmed = preg_replace('~^datastring=/mac_project~', '', $homepage1);

How to remove "�" from php string? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to replace � in a string
I am reading data from an XML sheet coming out of a database. In the raw output I am coming accross this character "�" which is the UTF-8 string meaning "�". Doing a simple search and remove with str_replace does not do the trick when searching for "�" or "�". Is there any other way to remove this character from a string?
UPDATE:
For reference this is the function that is cleaning up strings for me.
function db_utf8_convert($str)
{
$convmap = array(0x80, 0x10ffff, 0, 0xffffff);
return preg_replace('/\x{EF}\x{BF}\x{BD}/u', '', mb_encode_numericentity($str, $convmap, "UTF-8"));
}
You can do this:
$str = 'UTF-8 string meaning "�"';
echo preg_replace('/\x{EF}\x{BF}\x{BD}/u', '', iconv(mb_detect_encoding($str), 'UTF-8', $str));
Output: UTF-8 string meaning ""
You could do something similar to this:
<?php
$string = "asd fsa fsaf sf � asdfasdfs";
echo preg_replace("/[^\p{Latin} ]/u", "", $string);
Check out this script for more character matches:
http://www.regular-expressions.info/unicode.html#script
EDIT
I did find, this, people says it works, you could give it a try:
<?php
function removeBOM($str=""){
if(substr($str, 0,3) == pack("CCC",0xef,0xbb,0xbf)) {
$str=substr($str, 3);
}
return $str;
}
?>

Need to convert é to e in PHP [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
PHP remove accents
The title says most of it, but anyways...
I am getting data from an input file and inside the data we will have the character é. For our purposes we want to convert that to a regular lower case e.
Does anyone know how to do that?
I'd use this:
PHP Function: string strtr ( string $str , string $from , string $to )
from the PHP Site:
<?php
//In this form, strtr() does byte-by-byte translation
//Therefore, we are assuming a single-byte encoding here:
$addr = strtr($addr, "äåö", "aao");
?>
Just that one character? This seems too obvious... just replace it.
$str = "é";
$str = str_replace("é","e",$str);
echo $str; // "e"
http://php.net/manual/en/function.str-replace.php
$myText = str_replace( array('é'), array('e'), $myText);
$string = str_replace('é','e',$string);

Categories