need a function

Discussion in 'PHP' started by promotingspace.net, Sep 22, 2008.

  1. #1
    Hi
    I have the list of all countries in this format:
    ""=>Armenia,
    ""=>Aruba,
    ""=>Australia,
    ""=>Austria,
    ""=>Azerbaijan,
    ""=>Bahamas,
    ""=>Bahrain,
    ""=>Bangladesh,
    ""=>Barbados,
    I want to make it in the following format: (copy name between ""):
    "Afghanistan"=>Afghanistan,
    "Albania"=>Albania,
    "Algeria"=>Albania,
    "Andorra"=>Andorra,
    "Angola"=>Angola,
    "Anguilla"=>Anguilla,
    "Antigua an d Barbuda"=>Antigua an d Barbuda,
    "Argentina"=>Argentina,
    As the number of countries are a lot, how can i write a function to do the job for me?
    thanks for your help
     
    promotingspace.net, Sep 22, 2008 IP
  2. QiSoftware

    QiSoftware Well-Known Member

    Messages:
    805
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    158
    #2
    Using JavaScript -- use the indexOf function to search between the ">" and "," ... save the results and concatenate the results to the front of the string replacing the ""=. Sorry just noticed this was the PHP forum--- use the strstr function instead of the Javascript indexOf function-- see php.net.

    Q...
     
    QiSoftware, Sep 22, 2008 IP
  3. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #3
    what u mean by "Albania"=>Albania' is making it an array that has key "Albania" and value Albania?

    or u want it as a string.

    And, You want the script to process the list of countries (that you have now) only when you need it : makeArray()

    or

    you want to process the list of countries that you have now and print it as an array so that you can paste in your file? : makeArray_for_copy()

    kinda confusing sentences..

    EDIT: anyway test it using these simple functions. i assume the country list you have is a string. If it is, just make is as a variable like what i did.
    
    $lol = '""=>Armenia,
    ""=>Aruba,
    ""=>Australia,
    ""=>Austria,
    ""=>Azerbaijan,
    ""=>Bahamas,
    ""=>Bahrain,
    ""=>Bangladesh,
    ""=>Barbados,
    ';
    
    function makeArray($theString) {
    $lol = str_replace('""=>','',$theString);
    $lol = str_replace("\r\n",'',$lol);
    $lol = explode(',',$lol);
    foreach($lol as $lolx) {
    if (!empty($lolx)) {
    $lol_db[$lolx] = $lolx;
    }
    }
    return $lol_db;
    }
    
    function makeArray_for_copy($theString) {
    $lol = str_replace('""=>','',$theString);
    $lol = str_replace("\r\n",'',$lol);
    $lol = explode(',',$lol);
    foreach($lol as $lolx) {
    if (!empty($lolx)) {
    $lol_string .= '"'.$lolx.'" => \''.$lolx.'\',<br />';
    }
    }
    return $lol_string;
    }
    
    print '<pre>';
    print '<b>makeArray()</b><br />';
    // test the makeArray
    print_r (makeArray($lol));
    
    print '<br /><br /><b>makeArray_for_copy()</b><br />';
    // test the makeArray_for_copy
    print makeArray_for_copy($lol);
    
    print '</pre>';
    
    PHP:
     
    ads2help, Sep 22, 2008 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $list = '""=>Armenia,
    ""=>Aruba,
    ""=>Australia,
    ""=>Austria,
    ""=>Azerbaijan,
    ""=>Bahamas,
    ""=>Bahrain,
    ""=>Bangladesh,
    ""=>Barbados,
    ';
    $pattern = '/""=>([^,]+),/';
    $output = preg_replace($pattern,'"$1"=>$1,',$list);
    PHP:
    (it's a bit simpler than the above;))
     
    JAY6390, Sep 22, 2008 IP
  5. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #5
    regex, nice one XD
     
    ads2help, Sep 22, 2008 IP