This question already has an answer here:
How to decode something beginning with "\u" with PHP
(1 answer)
Closed 8 years ago.
I have one string:
"Hello\u00c2\u00a0World"
I would like convert in:
"Hello World"
I try :
str_replace("\u00c2\u00a0"," ","Hello\u00c2\u00a0World");
or
str_replace("\\u00c2\\u00a0"," ","Hello\u00c2\u00a0World");
but not work!
Resolve!
str_replace(chr(194).chr(160)," ","Hello\u00c2\u00a0World");
If you would like to remove \u.... like patterns then you can use this for example:
$string = preg_replace('/(\\\u....)+/',' ',$input);
You are most of the way there.
$stuff = "Hello\u00c2\u00a0World";
$newstuff = str_replace("\u00c2\u00a0"," ",$stuff);
you need to put the return from str_replace into a variable in order to do something with it later.
This should work
$str="Hello\u00c2\u00a0World";
echo str_replace("\u00c2\u00a0"," ",$str);
You may try this, taken from this answer
function replaceUnicode($str) {
return preg_replace_callback("/\\\\u00([0-9a-f]{2})/", function($m){ return chr(hexdec($m[1])); }, $str);
}
echo replaceUnicode("Hello\u00c2\u00a0World");
Related
This question already has answers here:
How substr function works? [closed]
(3 answers)
Closed 4 years ago.
I am trying to get "pa" from "a(bcdefghijkl(mno)pa)q".
This is my code for exampe:
$s = "a(bcdefghijkl(mno)pa)q";
$mystring = substr($s,14,15);
echo $mystring;
outputs is:
mno)pa)q
You have use right code but the second parameter is wrong. use this one below
$s = "a(bcdefghijkl(mno)pa)q";
$mystring = substr($s,14,2);
echo $mystring;
In substr function:
first parameter means from which position of string starts.
and the second parameter means how many characters you have want.
$s = "a(bcdefghijkl(mno)pa)q";
$mystring = substr($s,-4,2);
echo $mystring;
Try this
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;
?>
This question already has answers here:
how to do a sum on a string in php?
(5 answers)
Closed 8 months ago.
I was asked this question in a PHP test,
Question: How to get the sum of values?
$str = "1,2,3,4,5,6,7";
My Solution was:
// Splitting numbers in array and adding them up
$str = "1,2,3,4,5,6,7";
$num_array = explode(',', $str);
$str = 0;
foreach($num_array as $num) {
$str+=$num;
}
echo $str;
I was told that my solution is BAD, is it BAD? Can anyone please tell why is it BAD? And any better/best solutions?
Thanks, in advance
Well, your solution is correct, but they might be expecting optimized or efficient or smallest code. May be like this:
$str = "1,2,3,4,5,6,7";
echo array_sum(explode(',', $str));
you should use array_sum(explode(",",$yourarray)) after exploding the string rather than looping array . this would be more efficient.
This question already has answers here:
Extract a single (unsigned) integer from a string
(23 answers)
Closed 8 years ago.
$string = "comment-454";
I want to disply only 454? not comment-
i am saving in db comment-454 , now i want to crop comment-.. how can i
can someone provide me something, because i am newbie in php..
thanks in advance..
$string = str_replace("comment-", "", "comment-454");
should replace what you want.
you mentioned comment-454 came from your database. So ill assume it's in $result.
Now you can do:
$string = str_replace("comment-", "", $result);
echo $string;
Use explote() http://php.net/manual/de/function.explode.php
$teile = explode("-", $string);
echo $teile[1]; // 454
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);