怎么替换,js数组是用pHP输出吗?

解决方案 »

  1.   

    //js字串
    $str = <<<EOD
    //Contents for shopping 
    var shopping = new Array() 
    shopping[0]=' <a href="#">{head_clothing} </a>' 
    shopping[1]=' <a href="#">{head_bags} </a>' 
    shopping[2]=' <a href="#">{head_shoes} </a>' 
    shopping[3]=' <a href="#">{head_jewellery} </a>' 
    shopping[4]=' <a href="#">{head_accessories} </a>' //Contents for mystyle 
    var mystyle=new Array() 
    mystyle[0]=' <a href="#">{head_going_out} </a>' 
    mystyle[1]=' <a href="#">{head_working_girl} </a>' 
    mystyle[2]=' <a href="#">{head_casual} </a>' 
    mystyle[3]=' <a href="#">{head_cocktail_party} </a>' 
    mystyle[4]=' <a href="#">{head_dinner_date} </a>' 
    mystyle[5]=' <a href="#">{head_sportswear} </a>' //Contents for fashion 
    var fashion = new Array() 
    fashion[0]=' <a href="#">{head_cutting_edge_fashion} </a>' 
    fashion[1]=' <a href="#">{head_vintage} </a>' 
    fashion[2]=' <a href="#">{head_classic} </a>' //Contents for editor 
    var editor = new Array() 
    editor[0]=' <a href="#">{head_top_trends} </a>' 
    editor[1]=' <a href="#">{head_latest_arrivals} </a>' 
    editor[2]=' <a href="#">{head_for_him} </a>' 
    editor[3]=' <a href="#">{head_for_her} </a>' EOD;
    //替换变量
    $arr=array ( 'head_clothing' => 'Clothing', 
    'head_bags' => 'Bags', 
    'head_shoes' => 'Shoes', 
    'head_jewellery' => 'Jewellery', 
    'head_accessories' => 'Accessories', 'head_going_out' => 'Going out', 
    'head_working_girl' => 'Working girl', 
    'head_casual' => 'Casual', 
    'head_cocktail_party' => 'Cocktail Party', 
    'head_dinner_date' => 'Dinner Date', 
    'head_sportswear' => 'Sportswear (polo/gilf)', 'head_cutting_edge_fashion' => 'Cutting_edge fashion', 
    'head_vintage' => 'Vintage', 
    'head_classic' => 'Classic', 'head_top_trends' => 'Top trends', 
    'head_latest_arrivals' => 'Latest arrivals', 
    'head_for_him' => 'For Him', 
    'head_for_her' => 'For Her', ) 
    //替换变量
    foreach($arr as as $key => $value){
     $str =str_replace ($key ,$value,$str );
    }echo $str;
      

  2.   

    //替换变量
    foreach($arr as  $key => $value){
     $str =str_replace ('{'.$key.'}' ,$value,$str );
    }echo $str;