Solved

String manipulation (reverse and delete)

Posted on 2003-11-25
2
413 Views
Last Modified: 2006-11-17
Hi,

I want to transform string.
For example when I have this : "VOITURE (LA)" I want to obtain "LA-VOITURE".
So the steps are :
1. set the article at the beginning
2. delete the parenthesis
3. replace the space by a hyphen.

TIA
0
Comment
Question by:Richard Coffre
2 Comments
 
LVL 24

Expert Comment

by:shivsa
ID: 9823135
$str = '"VOITURE (LA)" ';
$first= 0;
$last = 0;
for ($i = 0; $i < strlen($str); $i++) {
  if($str[$i] == '(' ) {
     $first = $i ;
  if ($str[$i] == ')') {
     $last = $i ;
  }
}
$i=0;
for ($j=$first+1 ; $j < $last; $j++) {
  $rep[$i++] =  $str[$j];
}
$rep[$i+1] = '"-"';
for ($k=0; $k < $first ; $k++){
  $rep[$i++] = $str[$k];
}
     
0
 
LVL 14

Accepted Solution

by:
ThG earned 500 total points
ID: 9824204
*laughing*
I know that's it fast, lovely c-style code, and I appreciate it. But this is PHP.. so

<?php
$str = "VOITURE (LA)";
if (preg_match('/^(.*)\s+\((.*)\)$/', $str, $regp))
  $strnew = $regp[2] . "-" . $regp[1];
else
  print "Error: not in the expected format!\n";
print "$strnew\n";

Output is:
LA-VOITURE
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question