tags:

views:

94

answers:

5
+1  Q: 

PHP replace string

$name (string) gives something like (possible value):

John II, Litten Draw

We should update $name in two steps:

  1. Catch the words before first comma and throw them to the end of the string
  2. Remove first comma
  3. Create a file current_name.txt (or update if already exists) and throw to it source of $name

"John II, Litten Draw" should be replaced with "Litten Draw John II".

Thanks.

+4  A: 

Like this?

$split = explode(",", $name, 1);
$name = trim($split[1]) . " " . trim(split[0]);

Then it's just basic file I/O.

If you have a list of words (assuming they are all on separate lines):

$list = explode("\n", $names);
$nnames = "";
foreach($list as $name)
{
        $split = explode(",", $name);
        $nnames .= trim($split[1]) . " " . trim(split[0]) . "\n";
}
Josh K
will it work with more thank 2 words?
Happy
`file_put_contents` = easy way to dump string to file
Bob Fincheimer
@Ignatz: No. This will work with one word pair. I'll update to work with a list deliminated by a newline (`\n`).
Josh K
@Josh K seems a missunderstand. Sorry, have updated the question
Happy
-1 for not using regular expressions
stereofrog
Where does it say he wants it using regex? Also update your answer Josh to `split(",", $name, 1)` so it splits only the first comma
Bob Fincheimer
@Bob: Caught his answer update, thanks.
Josh K
@stereofog: Regular expressions aren't needed here.
Josh K
@Josh K: regexps are just the right tool for this job. Compare your code (5 function calls!) to mine.
stereofrog
@Josh K your code doesn' work at all
Happy
@Ignatz: Works fine for me. Note you have to handle the file I/O yourself.
Josh K
+1  A: 

See strpos to find the comma, ltrim to remove the whitespace, and fopen with the mode a to append to the file. You can also use explode to split around the comma, which is usually easier

Michael Mrozek
You don't need to find the comma. Split, trim, reconcat.
Josh K
@Josh Well, `explode` is "finding the comma" too, just not manually. I prefer `explode` too, but I was trying to give him exactly the bullets he asked for
Michael Mrozek
+2  A: 

This regex should do it for you...

preg_replace('#\\b(\\w+),\\s*(\\w+)\\b#', '\\2 \\1', $string);

Basically, it's looking for:

  1. A word boundry (the \\b part)
  2. Then one or more word characters (the \\w+ part)
  3. Then a comma followed by zero or more whitespace characters (,\\s*)
  4. Then one or more word characters (the \\w+ part)
  5. Finally, another word boundry...
ircmaxell
this doesn't work
Happy
Can you explain what you mean by "this doesn't work"? I've tried it on my machine, and it works quite well...
ircmaxell
+1  A: 

Here's some sample code that should work OK:

<?php

    function getCanonicalName($name) {

        // Check for the existance of a comma and swap 'elements' if necessary.
        if(strpos($name, ',') !== false) {
            list($surname, $forename) = explode(',', $name);
            $name = $forename . ' ' . $surname; 
        }

        // Trim the name.
        return trim($name);
    }   


    // Test data and file I/O.
    $outputData = '';
    $testData = array('Obama, Barak', 'Federer, Roger', 'John Parker');

    foreach($testData as $name) {
        $outputData .= getCanonicalName($name) . "\n";
    }


    file_put_contents('current_name.txt', $outputData, FILE_APPEND);
?>

Incidentally, this (like all of the solutions currently attached to your question) will cause data loss if there's more than one comma in $name. As such, if this is possible you should update getCanonicalName to cater for this eventuality.

middaparka
+2  A: 

regular expressions are the way to go here

$a = "Obama, Barak";
echo preg_replace('~(\w+)\W+(\w+)~', "$2 $1", $a);

also works for multiple names:

$a = "
Obama, Barak
Federer, Roger
Dickens, Charles
";

echo preg_replace('~(\w+)\W+(\w+)~', "$2 $1", $a);
stereofrog
this code replaces "Nadal, Rafael" into "nbsp Nadal;Rafael"
Happy
@Ignatz, i'm sure it doesn't ;))
stereofrog