Solved

String manipulation (reverse and delete)

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now