Solved

String manipulation (reverse and delete)

Posted on 2003-11-25
2
405 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

856 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