After imploding an array:
$in_list = "'".implode("','",$array)."'";
$in_list content is :
'Robert','Emmanuel','José','Alexander'
Now when i try to replace the word José by another string,
str_replace("José","J",$in_list);
It doesn't get the new value, José is still there. Am i missing something? thanx in advance.
How exactly do you try to replace the string?
When trying it this way:
$in_list = str_replace("José","J",$in_list);
echo $in_list;
everything should work fine.
Remember, the function is returning a value. So it returns a new string.
This should work. It depends on your array.
$str = array('Robert','Emmanuel','José','Alexander');
$str = implode(",", $str);
print str_replace('José', 'J', $str);
I'm not sure what's going on, It seems to work for me. What version of PHP are you using?
$in_list = "'".implode("','", array('Robert', 'Emmanuel', 'José', 'Alexander'))."'";
$replaced = str_replace("José", "J", $in_list);
//prints 'Robert','Emmanuel','J','Alexander'
echo $replaced;
See: http://codepad.viper-7.com/24qutm
try $in_list = html_entity_decode((str_replace(htmlentities("José"),"J",htmlentities($in_list));
Have you tried on a word without accents? I would say you have a character set mismatch, for example 'José' in $in_list is in latin1 character set and your PHP source file in UTF8.
If this is the case, you should first convert either your PHP file or the variable to the character set you want to work with.
Spontaneous guess: Those two strings are not the same. I suppose one "José" is a string hardcoded in your source code and the other is received from the database or the browser or so. If the encoding of the two strings is not the same, PHP won't identify them as identical and not replace the character. Make sure your source code file is saved in the same encoding as the data you're working on, preferably both being UTF-8.
This worked for me, but it doesn't look like I'm doing anything notably different from you?
$array = array('Robert', 'Emmanuel', 'José', 'Alexander');
$in_list = "'".implode("','",$array)."'";
echo $in_list.PHP_EOL;
echo str_replace("José","J",$in_list).PHP_EOL;
Output:
'Robert','Emmanuel','José','Alexander'
'Robert','Emmanuel','J','Alexander'
Keep in mind that str_replace will not perform the replacement on $in_list itself, but rather return a string containing the replacement.
Hope this helps!
Related
I'm trying to remove from a string variable, a character that appeared between spaces, I used some PHP functions to do it like str_replace but nothing happens.
An example below, I show it through var_dump PHP function :
string '�I�N�S�E�R�T� �[�d�b�o�]�
If anyone can tell me a way to do this, I'd be grateful.
That is a simple string translation operation (Demo):
$string = '�I�N�S�E�R�T� �[�d�b�o�]�';
echo strtr($string, array('�' => ''));
Output:
INSERT [dbo]
However you might have the problem to actually not knowing which character that is which then would require you to properly obtain the string first. So replacing it in the after would just be the wrong place.
You could try:
$str = '�I�N�S�E�R�T� �[�d�b�o�]�';
$filtered_str = filter_var($str, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH)
Demo. See: filter-var and filter.filters.sanitize
I'm a little confused here. if I pass a variable to json_decode, it doesn't work:
$stringJSON = $_GET['jsonstring'];
echo $stringJSON;
$stringObject = json_decode($stringJSON);
var_export($stringObject);
The first echo correctly shows me the JSON string I passed, e.g.
{\"Items\":[{\"Name\":\"name\",\"Description\":\"\"],\"Name\":\"Christmas\"}
The second echo shows NULL.
So I grab the string from the first echo and write the following code:
$stringObject = json_decode("{\"Items\":[{\"Name\":\"name\",\"Description\":\"\"],\"Name\":\"Christmas\"}");
var_export ($stringObject);
And what do you say, it shows me the correctly decoded array. The string is absolutely the same, I even kept the escape characters. Or maybe they are the problem?
Looks like your server has magic_quotes_gpc enabled. Either disable it or run $stringJSON through stripslashes() before using it.
$stringJSON = get_magic_quotes_gpc() ?
stripslashes($_GET['jsonstring']) : $_GET['jsonstring'];
This
[{\"Name\":\"name\",\"Description\":\"\"]
needs to be
[{\"Name\":\"name\",\"Description\":\"\"}]
You are missing the closing }
If it shows you a string with slashes in it when you echo it, that means the string has slashes in it. That's not a valid JSON string, the slashes don't belong there. If you paste that string into PHP, the slashes are evaluated by PHP. The string literal "\"" in PHP source code evaluates to the string ", so the slashes are effectively removed and you are decoding a valid JSON string.
I suspect you have Magic Quotes on which are inserting the slashes into GET values, turn them off.
This is a quoting problem: Try the following
$stringObject = json_decode("{\"Items\":[{\"Name\":\"name\",\"Description\":\"\"],\"Name\":\"Christmas\"}");
echo $stringObject;
var_export ($stringObject);
as you see, the $stringObject has no quotes (but the one coming from $_GET has them)
so you might need
$stringJSON = $_GET['jsonstring'];
$stringObject = json_decode(stripslashes($stringJSON));
var_export($stringObject);
run json_decode twice.
$str = json_decode($jsonData,true);
$str = json_decode($str,true);
It may help someone.
json_encode($str, JSON_UNESCAPED_SLASHES);
it may help you.
This may seem odd but I'm simply trying to put a url together.
The first part ($first) I get from user input using strrpos() and substr() with "/".
The exact file I want to get to is fixed ($second) so all I think I need to do is this:
$first = "http://www.somedomain.de/somepath/";
$second = "thexml.xml";
$url = $first.$second;
BUT: Although I use trim() on every part there still is some whitespace between the two parts when I print $url.
When I try to navigate to $url the whitespace is replaced by a "%".
The path itself is correct, when I get rid of the whitespace/ % manually in my browser's adress bar.
I also tried putting the two strings together with an array and implode() but the output stays the same.
What am I doing wrong?
Update from Lisa
ok, so I printed $first and $second separately and there are no whitespaces. it seems to be appearing when I concatenate them and exactly where the two strings are put together.
Any other ideas?
ok, so I printed $first and $second separately and there are no whitespaces. it seems to be appearing when I concatenate them and exactly where the two strings are put together.
Any other ideas?
Most likely, the code you're editing or the inputs (user, database, etc.) is not what you expect. Try trimming down (no pun intended) the code to a minimal example. For example, go from
$first = "http://www.somedomain.de/somepath/";
$second = "thexml.xml";
$url = $first.$second;
echo $url; // No space
to
$first = $_POST['url'];
$second = "thexml.xml";
$url = $first.$second;
echo $url; // If this contains a space, the input contains the offending space
step-by-step to find the mistake.
Have you tried rawurlencode? http://php.net/rawurlencode
I know this is a very old question but I just had this similar issue and was finally able to find a solution.
My solution is to use the trim function like this:
$url = trim($first).$second;
I want to to know how can I convert a word into unicode exactly like:
http://www.arabunic.free.fr/
can anyone know how to do that using PHP considering that Arabic text may contains ligatures?
thanks
Edit
I'm not sure what is that "unicode" but I need to have the Arabic Character in it's equivalent machine number considering that arabic characters have different contextual forms depending on their position - see here:
http://en.wikipedia.org/wiki/Arabic_alphabet#Table_of_basic_letters
the same character in different position:
ب | ـب | ـبـ | بـ
I think it must be a way to convert each Arabic character into it's equivalent number, but how?
Edit
I still believe there's a way to convert each character to it's form depending on positions
any idea is appreciated..
All what you need is function called: utf8Glyphs which you can find it in ArGlyphs.class.php download it from ar-php
and visit Ar-PHP for the ArPHP more information about the project and classes.
This will reverse the word with same of its characters (glyphs).
Example of usage:
<?php
include('Arabic.php');
$Arabic = new Arabic('ArGlyphs');
$text = 'بسم الله الرحمن الرحيم';
$text = $Arabic->utf8Glyphs($text);
echo $text;
?>
i assume you wnat to convert بهروز to \u0628\u0647\u0631\u0648\u0632 take a look at http://hsivonen.iki.fi/php-utf8/ all you have to do after calling unicodeToUtf8('بهروز') is to convert integers you got in array to hex & make sure they have 4digigts & prefix em with \u & you're done. also you can get same using json_encode
json_encode('بهروز') // returns "\u0628\u0647\u0631\u0648\u0632"
EDIT:
seems you want to get character codes of بب which first one differs from second one, all you have to do is applying bidi algorithm on your text using fribidi_log2vis then getting character code by one of ways i said before.
here's example:
$string = 'بب'; // \u0628\u0628
$bidiString = fribidi_log2vis($string, FRIBIDI_LTR, FRIBIDI_CHARSET_UTF8);
json_encode($bidiString); // \ufe90\ufe91
EDIT:
i just remembered that tcpdf has bidi algorithm which implemented using pure php so if you can not get fribidi extension of php to work, you can use tcpdf (utf8Bidi by default is protected so you need to make it public)
require_once('utf8.inc'); // http://hsivonen.iki.fi/php-utf8/
require_once('tcpdf.php'); // http://www.tcpdf.org/
$t = new TCPDF();
$text = 'بب';
$t->utf8Bidi(utf8ToUnicode($text)); // will return an array like array(0 => 65168, 1 => 65169)
Just set the element containing the arabic text to "rtl" (right to left), then input correctly spelled arabic and the text will flow with all ligatures looked for.
div {
direction:rtl;
}
On a side note, don't forget to read "The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)"
Think about that : The "ba" (ب) arabic letter is a "ba" no matter where it appears in the sentence.
Try this:
<?php
$string = 'a';
$expanded = iconv('UTF-8', 'UTF-32', $string);
$arr = unpack('L*', $expanded);
print_r($arr);
?>
I'm totally agree with FloatBird about the use of the arabic.php which you will find it as he said at ar-php, The thing is they have changed the class name after version 4 from Arabic to I18N_Arabic so in order for the code to work using arabic.php ver 4.0 you need to change the code to
<?php
include('Arabic.php');
$Arabic = new I18N_Arabic('ArGlyphs');
$text = 'بسم الله الرحمن الرحيم';
$text = $Arabic->utf8Glyphs($text);
echo $text;
?>
Also notice that you need to put the php code file inside the I18N folder.
Anyway it is working fantastically, Thanks again FloatBird
I had a similar problem when I wanted to store an object that had values in Arabic, so writing in Arabic was stored as UNICODE," so the solution was as follows.
$detailsLog = $product->only(['name', 'unit', 'quantity']);
$detailsLog = json_encode($detailsLog, JSON_UNESCAPED_UNICODE);
$log->details = $detailsLog;
$log->save();
When you put the second parameter of the json_encode JSON_UNESCAPED_UNICODE follower, the Arabic words return without encoding.
i think you could try:
<meta charset="utf-8" />
if this does not work use FloatBird Answer
I'm trying to remove this character, and this character only from a $string with php. I've tried str_replace("»","",$test) and str_replace(chr(187),"",$test) but they can't touch it. The issue is it isn't in the same spot every time, so I can't even get creative with trimming the ends.
Are you forgetting that str_replace(old, new, string) doesn't modify the original string, but rather returns a copy of the modified string?
So:
$string = "This is the » character";
$new_string = str_replace("»", "_", $string);
echo $new_string;
Should work (it does for me)!
Wanted to point out that "»" in HTML equals » which is a standard. So, my advise would be that you better use standard characters.
Reference of characters: http://www.w3schools.com/tags/ref_entities.asp
I ended up stepping through the string one character at a time with
echo ord($test[n]
until I finally found the offending hidden character that was ruining my night.
Thanks for all the help everyone!