PHP - String manipulation remove spcial characters and replace spaces

后端 未结 5 1413
余生分开走
余生分开走 2020-12-15 14:57

I am getting strings from a database and then using the strings to build a URL. My issue is, some of the strings will have characters like < > & { } * general specia

5条回答
  •  孤街浪徒
    2020-12-15 15:20

    $search_value =array(",",".",'"',"'","\\"," ","/","&","[","]","(",")"," 
    {","}",":","`","!","@","#","%","=","+");
    $replace_value =array("-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","- 
    ","-","-","-","-","-");
    $product_name = str_replace($search_value,$replace_value,$row["Product_Name"]);
    $product_name = str_replace("--","-",$product_name);
    $product_name = str_replace("--","-",$product_name);
    $product_name = preg_replace('/-$/', '', $product_name);
    $product_name = preg_replace('/^-/', '', $product_name);
    

    This will create dashed string (Only have alphanumeric character with dash).Useful for create URI strings.

提交回复
热议问题