Jump to content


Photo

reverse a string



  • Please log in to reply
6 replies to this topic

#1 levyta

levyta

    Member

  • Members
  • PipPip
  • 11 posts

Posted 01 September 2013 - 04: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

    Wiser? Not exactly.

  • Gurus
  • 2,709 posts
  • LocationBonita, FL

Posted 01 September 2013 - 04: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.
Recycle your old CD's, don't trash them!
Did I help you out?  Feeling generous? I accept tips via Paypal or Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7

#3 levyta

levyta

    Member

  • Members
  • PipPip
  • 11 posts

Posted 24 September 2013 - 02: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

    Hello World

  • Members
  • PipPipPip
  • 874 posts
  • LocationUK
  • Age:22

Posted 24 September 2013 - 02: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 17 - PHP 5.4 - MySQL 5 - Win7 [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

    Cracka Memba

  • Gurus
  • 1,889 posts
  • LocationThe Republic of Texas

Posted 24 September 2013 - 03: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

    Cracka Memba

  • Gurus
  • 1,889 posts
  • LocationThe Republic of Texas

Posted 24 September 2013 - 03: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

    Member

  • Members
  • PipPip
  • 11 posts

Posted 25 September 2013 - 01:52 PM   Best Answer

Works great!

Thanks






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com