Jump to content


Photo

reverse a string



  • Please log in to reply
6 replies to this topic

#1 levyta

levyta
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 01 September 2013 - 09:37 AM

I have a sting, containing numbers and letters,

 such that:

 

abc  12,345 def

 

I want reversing only the words, so the result will look:

def 12,345 abc.

 

 

I know how to reverse the all string:

$array = explode("    ", $string);
foreach ($array as &$word) { 
   $word = strrev($word);
}
$rev_string = implode(" ", $array);

 

 

but don't know how to deal with the numbers.

 

Thanks
 



#2 kicken

kicken
  • Gurus
  • Wiser? Not exactly.
  • 3,312 posts
  • LocationBonita, FL

Posted 01 September 2013 - 09:52 AM

Based on your example, explode() the string into individual words, array_reverse the result to reverse the order of the words, then implode it back together again. No need to loop the array or use strrev.
Did I help you out? Feeling generous? I accept tips via Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7
Kicken's World⦄ ⦃Recycle old CD's

#3 levyta

levyta
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 24 September 2013 - 07:28 PM

Thanks for the answer.

What if I want to reverse only the letters?

e.g.,

$string = "abc  12,345 def"

 

I want to have: 

$string = "cba 12,345 fed"



#4 PaulRyan

PaulRyan
  • Members
  • PipPipPip
  • Hello World
  • 878 posts
  • LocationUK
  • Age:23

Posted 24 September 2013 - 07:52 PM

Try this

<?PHP

  $string = "abc  12,345 def";
 
  $stringElements = explode(' ', $string);
 
  foreach($stringElements AS $key => $element) {
    if(preg_match('/[a-z]/', $element)) {
      $stringElements[$key] = strrev($element);
    }
  }
 
  echo implode(' ', $stringElements);
 
?>

My Internet Home - Dumping ground for new ideas/tips/tricks and general grievances of developing.

 

Firefox 38 - PHP 5.6 - MySQL 5.6 - Win8 [64-bit]

Rule for asking for help: If you don't show that you've tried to accomplish something, you don't deserve help.


#5 AbraCadaver

AbraCadaver
  • Gurus
  • Creepy Ass Cracka Memba
  • 1,893 posts
  • LocationThe Republic of Texas

Posted 24 September 2013 - 08:29 PM

PHP >= 5.3.0

$new_string = preg_replace_callback('/[a-zA-Z]+/', function($m) { return strrev($m[0]); }, $string);

PHP < 5.3.0

$new_string = preg_replace('/([a-zA-Z]+)/e', 'strrev("$1")', $string);

mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

#6 AbraCadaver

AbraCadaver
  • Gurus
  • Creepy Ass Cracka Memba
  • 1,893 posts
  • LocationThe Republic of Texas

Posted 24 September 2013 - 08:45 PM

Or, assuming a constant pattern for your string:

$new_string = strrev(strtok($string, ' ')) . ' ' . strtok(' '). ' ' . strrev(strtok(''));

mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

#7 levyta

levyta
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 25 September 2013 - 06:52 PM   Best Answer

Works great!

Thanks






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users