SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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.

    Code:
    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;
    }

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    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: strrev
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,222
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    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: strrev
    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.

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    thanks, it works.
    ineed i had to take utf-8 in mind, so i found this func in some website which does the trick.
    Code:
    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;
    }

  5. #5
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Atlanta, GA
    Posts
    244
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Another easy way would be to do something like the following -- which I believe is UTF-8 safe:
    PHP Code:
    $text implode(array_reverse(str_split($text))); 

  6. #6
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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

    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

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

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The source text would not originally been scraped from an LTR encoded webpage now, would it?

  8. #8
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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:
    Code:
    imagefttext($template, 30, 0,0, 50, 0, 'gyadl.ttf', 'something'); // results in 'gnihtemos' being printed

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

  10. #10
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •