Changing the order of a string

Hello everyone,

When i try to write a string in non english using php it gets written in the opposite direction, for example:
if i write: “example”, it’ll write “elpmaxe”

So i wanna build an php function which will correct the string to be “example”

Anyone can help me on this?
I’ve tried the following, but it didnt work.


function reorder_string($str)
{
	$j = mb_strlen('UTF-8', $str);
	for ($i = 0;$i < $j;$i++,$j--)
	{
		$temp = $str[$i];
		$str[$i] = $str[$j];
		$str[$j] = $temp;
	}
	return $str;
}

what string are you trying to write that it puts it out backwards o.O never heard of that behavior.

anyway, dont overcomplicate things. PHP already supplies a method to do this: [FPHP]strrev[/FPHP]

Be careful with strrev though, as it may not support utf-8 characters. However, if you read the first few comments on the manual page listed above, there are UTF-8 compatible methods in the comments.

thanks, it works.
ineed i had to take utf-8 in mind, so i found this func in some website which does the trick.

function mb_strrev($text)
{
	$funcParams = array($text);
	$funcParams[] = 'UTF-8';
	$length = call_user_func_array('mb_strlen', $funcParams);
	$output = '';
	$funcParams = array($text, $length, 1);
	$funcParams[] = 'UTF-8';
	while ($funcParams[1]--)
		$output .= call_user_func_array('mb_substr', $funcParams);
	return $output;
}

Another easy way would be to do something like the following – which I believe is UTF-8 safe:


$text = implode(array_reverse(str_split($text)));

kduv
Another easy way would be to do something like the following – which I believe is UTF-8 safe:

Tested and its not UTF-8 safe :slight_smile:

Anyways, I’ve encountered an issue with this, and that got it abit more complicated.
I noticed that numbers, special chars and english letters are all printed just fine, its only my language which is printed in a reversed order.
So im wondering, how can i modify the below function to not reverse english letters, numbers and symbols, and ofcourse keep the order of the sentence as original.
example:
su llac 01234567
should become:
call us 01234567

function mb_strrev($text)
{
	$length = mb_strlen($text, 'UTF-8');
	$output = '';
	while ($length--)
		$output .= mb_substr($text, $length, 1, 'UTF-8');
	return $output;
}

The source text would not originally been scraped from an LTR encoded webpage now, would it?

Its not related to RTL, in regular HTML files the text prints just fine.
I got the problem when i try to put some text as image using imagettftext such as in the following example:

imagefttext($template, 30, 0,0, 50, 0, 'gyadl.ttf', 'something'); // results in 'gnihtemos' being printed

Well, did you try the same thing but using a different font? Maybe the font is the cause of your problem?

I’m sorry if my idea seems silly to you, but I do not know any more about what would cause this issue if isolating this variable (the font) does not lead you any closer to fixing it.

Tried many different fonts, all are printed in reversed order.
It seems like i must reverse the string, but it also seems like that making numbers and symbols not messing it up is kinda hard.

So my final solution is going to be to remove all symbols & numbers from the string before reversing it.