Solved

String manipulation (reverse and delete)

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

777 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