mb_strtolower not working - php

I have the following string:
$var = "RUA TANGARA"
And I'm doing:
echo mb_strtolower(preg_replace('/[^~\'"]/', null, iconv('UTF-8', 'ASCII//TRANSLIT', $var)), 'UTF-8');
But this still returning "RUA TANGARA".
I use the preg_replace() because $var can be "RÜÁ TÃNAGARA".
Can someone help me?

The issue is that you want to replace certain characters after the transliteration but you specified ^ (which has special meaning and means NOT) at the beginning of the character class [].
So you are replacing characters that are NOT ~'" (which happens to be all of them in your example), so it results in an empty string. To fix, just escape the ^, move it away from the beginning or remove it if not needed and it should be fine:
/[\^~\'"]/
Or:
/[~^\'"]/
Working Example

PHP's functions work very well, take a look at this simple demonstration:
<?php
$data = "RUA TANGARA";
$result = mb_strtolower($data);
var_dump($result);
The obvious output is:
string(11) "rua tangara"
The same works with non ascii characters:
<?php
$data = 'RÜÁ TÃNAGARA';
$result = mb_strtolower($data);
var_dump($result);
The output of that is:
string(15) "rüá tãnagara"

Try
$newStr = strtolower($var);
echo $newStr;

Related

How to return {"a": "\u0002"} to browser in php?

I want to send some data to browser, I write code like below.
<?php
$arr = array('a'=> "\u0002 hello");
exit(json_encode($arr));
?>
and I get a result:
but I wanna a result like below, \u0002 not \\u0002, what should I do?
"\u0002" in PHP is not the character with code 2 (why do you use it?) but a string that contains \, u and the four digits.
Use the chr() function to produce a character from its numeric code.
$arr = array('a'=> chr(2)." hello");
echo(json_encode($arr));
Check it online.
\u#### is JSON's unicode escape format, and has no meaning in a PHP string.
Using PHP's actual unicode escape:
$arr = array('a'=> "\u{0002} hello");
Or, since codepoints below 128 / 0x80 have single-byte representations, you can get away with:
$arr = array('a'=> "\x02 hello");
Both will produce the desired output: {"a":"\u0002 hello"}

PHP str_replace removing unintentionally removing Chinese characters

i have a PHP scripts that removes special characters, but unfortunately, some Chinese characters are also removed.
<?php
function removeSpecialCharactersFromString($inputString){
$inputString = str_replace(str_split('#/\\:*?\"<>|[]\'_+(),{}’! &'), "", $inputString);
return $inputString;
}
$test = '赵景然 赵景然';
print(removeSpecialCharactersFromString($test));
?>
oddly, the output is 赵然 赵然. The character 景 is removed
in addition, 陈 一 is also removed. What might be the possible cause?
The string your using to act as a list of the things you want to replace doesn't work well with the mixed encoding. What I've done is to convert this string to UTF16 and then split it.
function removeSpecialCharactersFromString($inputString){
$inputString = str_replace(str_split(
mb_convert_encoding('#/\\:*?\"<>|[]\'_+(),{}’! &', 'UTF16')), "", $inputString);
return $inputString;
}
$test = '#赵景然 赵景然';
print(removeSpecialCharactersFromString($test));
Which gives...
赵景然赵景然
BTW -str_replace is MB safe - sort of recognised the poster... http://php.net/manual/en/ref.mbstring.php#109937

How to remove chinese characters in a string

is there any easy way to truncate chinese characters i found that regexp but it doesn't work as expected
<?php
$data1='疯狂的管道Test';
$data2='睡眠帮手-背景乐Test';
echo str_replace(preg_replace("/[^\x{4e00}-\x{9fa5}]+/u", '', $data1),'',$data1)
."<br>\n".
str_replace(preg_replace("/[^\x{4e00}-\x{9fa5}]+/u", '', $data2),'',$data2);
exit;
it works for data1 but not data2
You can use a Unicode character property (Han should work for you):
preg_replace("/\p{Han}+/u", '', $data)
Working example: http://ideone.com/uEiIV5
Try this code (online version # Ideone.com):
<?php
$data1='疯狂的管道Test';
$data2='睡眠帮手-背景乐Test';
echo preg_replace("/[\x{4e00}-\x{9fa5}]+/u", '', $data1), "\n";
echo preg_replace("/[\x{4e00}-\x{9fa5}]+/u", '', $data2);
// Better use this (credits to Kobi's answer below)
preg_replace("/\p{Han}+/u", '', $data)
I have removed the ^ from the regular expression so we don't need str_replace() anymore.
Your old regexp matched all non-chinese characters thus preg_replace() only left chinese character in the returned string. In order to obtain the final result, you had to replace the found chinese characters by an empty string.
preg_replace("/[^\x{4e00}-\x{9fa5}]+/u", '', $data1) // returns 疯狂的管道
str_replace('疯狂的管道', '', $data1); // gives us Test
The second regexp again matched all non-chinese characters. But now, they are not in a sequence!
preg_replace("/[^\x{4e00}-\x{9fa5}]+/u", '', $data2) // returns 睡眠帮手背景乐
And this string cannot be found in $data2 anymore thus it doesn't work.
This one should also do the job
/[^\u4E00-\u9FFF]+/

Replace a character only in one special part of a string

When I've a string:
$string = 'word1="abc.3" word2="xyz.3"';
How can I replace the point with a comma after xyz in xyz.3 and keep him after abc in abc.3?
You've provided an example but not a description of when the content should be modified and when it should be kept the same. The solution might be simply:
str_replace("xyz.", "xyz", $input);
But if you explicitly want a more explicit match, say requiring a digit after the ful stop, then:
preg_replace("/xyz\.([0-9])+/", 'xyz\${1}', $input);
(not tested)
something like (sorry i did this with javascript and didn't see the PHP tag).
var stringWithPoint = 'word1="abc.3" word2="xyz.3"';
var nopoint = stringWithPoint.replace('xyz.3', 'xyz3');
in php
$str = 'word1="abc.3" word2="xyz.3"';
echo str_replace('xyz.3', 'xyz3', $str);
You can use PHP's string functions to remove the point (.).
str_replace(".", "", $word2);
It depends what are the criteria for replace or not.
You could split string into parts (use explode or preg_split), then replace dot in some parts (eg. str_replace), next join them together (implode).
how about:
$string = 'word1="abc.3" word2="xyz.3"';
echo preg_replace('/\.([^.]+)$/', ',$1', $string);
output:
word1="abc.3" word2="xyz,3"

PHP Remove everything after and including .html in a web address string

I'm trying to remove everything after and including '.html' in a web address string. Current (failing) code is:
$input = 'http://example.com/somepage.html?foo=bar&baz=x';
$result = preg_replace("/(.html)[^.html]+$/i",'',$input);
Desired outcome:
value of $result is 'http://example.com/somepage'
Some other examples of $input that should lead to same value $result:
http://example.com/somepage
http://example.com/somepage.html
http://example.com/somepage.html?url=http://example.com/index.html
Your regular expresson is wrong, it would only match strings ending with <one char> "html" <one or more chars matching ., h, t, m or l>. Since preg_replace just returns the string "as-is" if there was no match, you'd be fine with matching the literal .html and ignoring anything after it:
$result = preg_replace('/\.html.*/', '', $input);
Why not use parse_url instead?
If you ever have issues with the syntax for preg_replace() then you can also use explode():
$input = explode(".html", $input);
$result = $input[0];

Categories