diff --git a/htdocs/admin/agenda_xcal.php b/htdocs/admin/agenda_xcal.php
index 2b5cdd0e8248f31aee059d8e8680509904d9490d..2b5d2eab08107cc8176e71701bb06e931f761df0 100644
--- a/htdocs/admin/agenda_xcal.php
+++ b/htdocs/admin/agenda_xcal.php
@@ -120,8 +120,7 @@ print "<br>";
 
 // Show message
 $message='';
-$pattern = str_replace('/','\/',DOL_URL_ROOT); // Add backslashes for regular expression
-$urlwithouturlroot=preg_replace('/'.$pattern.'$/i','',$dolibarr_main_url_root);
+$urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',$dolibarr_main_url_root);
 $urlvcal='<a href="'.DOL_URL_ROOT.'/comm/action/agendaexport.php?format=vcal&exportkey='.($conf->global->MAIN_AGENDA_XCAL_EXPORTKEY?urlencode($conf->global->MAIN_AGENDA_XCAL_EXPORTKEY):'...').'" target="_blank">'.$urlwithouturlroot.DOL_URL_ROOT.'/comm/action/agendaexport.php?format=vcal&exportkey='.($conf->global->MAIN_AGENDA_XCAL_EXPORTKEY?urlencode($conf->global->MAIN_AGENDA_XCAL_EXPORTKEY):'KEYNOTDEFINED').'</a>';
 $message.=$langs->trans("WebCalUrlForVCalExport",'vcal',$urlvcal);
 $message.='<br>';
diff --git a/htdocs/admin/paybox.php b/htdocs/admin/paybox.php
index 5d69bde5fc820fa2622e7b0bb9b78d7c67f1a672..f89c85f1146d88a5c4eeb6a4ff1ae92311c7bf4e 100644
--- a/htdocs/admin/paybox.php
+++ b/htdocs/admin/paybox.php
@@ -160,9 +160,7 @@ print '<br><br>';
 print '<u>'.$langs->trans("FollowingUrlAreAvailableToMakePayments").':</u><br>';
 // Should work with DOL_URL_ROOT='' or DOL_URL_ROOT='/dolibarr'
 $firstpart=$dolibarr_main_url_root;
-$pattern = str_replace('/','\/',DOL_URL_ROOT); // Add backslashes for regular expression
-$regex='/'.$pattern.'$/i';
-$firstpart=preg_replace($regex,'',$firstpart);
+$firstpart=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',$firstpart);
 print '<br>';
 print img_picto('','puce.png').' '.$langs->trans("ToOfferALinkForOnlinePaymentOnFreeAmount",$servicename).':<br>';
 print '<b>'.$firstpart.DOL_URL_ROOT.'/public/paybox/newpayment.php?amount=<i>9.99</i>&tag=<i>your_free_tag</i></b>'."<br>\n";
diff --git a/htdocs/admin/webcalendar.php b/htdocs/admin/webcalendar.php
index e1790a6b50e8613ac4c0384d7857ce95cf747ea2..dba39f15403b77209c28b69626dcbaac04498e34 100644
--- a/htdocs/admin/webcalendar.php
+++ b/htdocs/admin/webcalendar.php
@@ -313,8 +313,7 @@ print "<br>";
 
 // Show message
 $message='';
-$pattern = str_replace('/','\/',DOL_URL_ROOT); // Add backslashes for regular expression
-$urlwithouturlroot=preg_replace('/'.$pattern.'$/i','',$dolibarr_main_url_root);
+$urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',$dolibarr_main_url_root);
 $urlvcal='<a href="'.DOL_URL_ROOT.'/webcal/webcalexport.php?format=vcal&exportkey='.$conf->global->PHPWEBCALENDAR_PASSWORD_VCALEXPORT.'" target="_blank">'.$urlwithouturlroot.DOL_URL_ROOT.'/webcal/webcalexport.php?format=vcal&exportkey='.$conf->global->PHPWEBCALENDAR_PASSWORD_VCALEXPORT.'</a>';
 $message.=$langs->trans("WebCalUrlForVCalExport",'vcal',$urlvcal);
 $message.='<br>';
diff --git a/htdocs/admin/webservices.php b/htdocs/admin/webservices.php
index 75734829918faa877feee7289999158075157c5f..6381349df9d6649b2fb292dbbde812130fa6f5e3 100644
--- a/htdocs/admin/webservices.php
+++ b/htdocs/admin/webservices.php
@@ -92,9 +92,7 @@ print '<br><br>';
 
 // Should work with DOL_URL_ROOT='' or DOL_URL_ROOT='/dolibarr'
 $firstpart=$dolibarr_main_url_root;
-$pattern = str_replace('/','\/',DOL_URL_ROOT); // Add backslashes for regular expression
-$regex='/'.$pattern.'$/i';
-$firstpart=preg_replace($regex,'',$firstpart);
+$firstpart=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',$firstpart);
 
 print '<u>'.$langs->trans("WSDLCanBeDownloadedHere").':</u><br>';
 $url=$firstpart.DOL_URL_ROOT.'/webservices/server.php?wsdl';
diff --git a/htdocs/categories/categorie.class.php b/htdocs/categories/categorie.class.php
index a9fd66fa19abcac652d79b0f79e46ece16161176..02e9339b8672cdd9ef9ff8de83c4a557174f2a04 100644
--- a/htdocs/categories/categorie.class.php
+++ b/htdocs/categories/categorie.class.php
@@ -1091,8 +1091,7 @@ class Categorie
 	{
 		$dir = dirname($file).'/'; // Chemin du dossier contenant l'image d'origine
 		$dirthumb = $dir.'/thumbs/'; // Chemin du dossier contenant la vignette
-		$dir = str_replace('/','\/',$dir); // Add backslashes for regular expression
-		$filename = preg_replace('/'.$dir.'/i','',$file); // Nom du fichier
+		$filename = preg_replace('/'.preg_quote($dir,'/').'/i','',$file); // Nom du fichier
 
 		// On efface l'image d'origine
 		dol_delete_file($file,1);
diff --git a/htdocs/includes/menus/barre_top/eldy_backoffice.php b/htdocs/includes/menus/barre_top/eldy_backoffice.php
index 07dfed3d796e9dff77c918ba80b7183914665dec..e119e82fa790d2ac224e626ce25f33e13040de55 100644
--- a/htdocs/includes/menus/barre_top/eldy_backoffice.php
+++ b/htdocs/includes/menus/barre_top/eldy_backoffice.php
@@ -292,8 +292,8 @@ class MenuTop {
 		if (! empty($conf->telephonie->enabled) && $user->rights->telephonie->lire)
 		{
 			$class="";
-			$pattern = str_replace('/','\/',DOL_URL_ROOT); // Add backslashes for regular expression
-			if (preg_match("/^".$pattern."\/telephonie\//",$_SERVER["PHP_SELF"]))
+			$path = DOL_URL_ROOT.'/telephonie/';
+			if (preg_match("/^".preg_quote($path,'/')."/i",$_SERVER["PHP_SELF"]))
 			{
 				$class='class="tmenusel"'; $_SESSION['idmenu']='';
 			}
@@ -313,8 +313,8 @@ class MenuTop {
 		{
 			$langs->load("energy");
 			$class="";
-			$pattern = str_replace('/','\/',DOL_URL_ROOT); // Add backslashes for regular expression
-			if (preg_match("/^".$pattern."\/energie\//",$_SERVER["PHP_SELF"]))
+			$path = DOL_URL_ROOT.'/energie/';
+			if (preg_match('/^'.preg_quote($path,'/').'/i',$_SERVER["PHP_SELF"]))
 			{
 				$class='class="tmenusel"'; $_SESSION['idmenu']='';
 			}
diff --git a/htdocs/includes/menus/barre_top/eldy_frontoffice.php b/htdocs/includes/menus/barre_top/eldy_frontoffice.php
index 0d14b9d9a08e692dcbf587d70f622fcaef30c79d..f8b8d620e027208d6c6ddc3491fb30027c4e49a6 100644
--- a/htdocs/includes/menus/barre_top/eldy_frontoffice.php
+++ b/htdocs/includes/menus/barre_top/eldy_frontoffice.php
@@ -292,8 +292,8 @@ class MenuTop {
 		if (! empty($conf->telephonie->enabled) && $user->rights->telephonie->lire)
 		{
 			$class="";
-			$pattern = str_replace('/','\/',DOL_URL_ROOT); // Add backslashes for regular expression
-			if (preg_match("/^".$pattern."\/telephonie\//",$_SERVER["PHP_SELF"]))
+			$path = DOL_URL_ROOT.'/telephonie/';
+			if (preg_match('/^'.preg_quote($path,'/').'/i',$_SERVER["PHP_SELF"]))
 			{
 				$class='class="tmenusel"'; $_SESSION['idmenu']='';
 			}
@@ -313,8 +313,8 @@ class MenuTop {
 		{
 			$langs->load("energy");
 			$class="";
-			$pattern = str_replace('/','\/',DOL_URL_ROOT); // Add backslashes for regular expression
-			if (preg_match("/^".$pattern."\/energie\//",$_SERVER["PHP_SELF"]))
+			$path = DOL_URL_ROOT.'/energie/';
+			if (preg_match('/^'.preg_quote($path,'/').'/i',$_SERVER["PHP_SELF"]))
 			{
 				$class='class="tmenusel"'; $_SESSION['idmenu']='';
 			}
diff --git a/htdocs/includes/menus/barre_top/rodolphe.php b/htdocs/includes/menus/barre_top/rodolphe.php
index e37d362211d513f5a772ef7e412afb1b42f89e52..d39298482305766e04538fc3496ced3f5f85d702 100644
--- a/htdocs/includes/menus/barre_top/rodolphe.php
+++ b/htdocs/includes/menus/barre_top/rodolphe.php
@@ -249,8 +249,8 @@ class MenuTop {
         if ($conf->telephonie->enabled && $user->rights->telephonie->lire)
         {
             $class="";
-            $pattern = str_replace('/','\/',DOL_URL_ROOT); // Add backslashes for regular expression
-            if (preg_match("/^".$pattern."\/telephonie\//",$_SERVER["PHP_SELF"]))
+            $path = DOL_URL_ROOT.'/telephonie/';
+            if (preg_match('/^'.preg_quote($path,'/').'/i',$_SERVER["PHP_SELF"]))
             {
                 $class='class="tmenusel"'; $_SESSION['idmenu']='';
             }
@@ -268,8 +268,8 @@ class MenuTop {
         {
             $langs->load("energy");
             $class="";
-            $pattern = str_replace('/','\/',DOL_URL_ROOT); // Add backslashes for regular expression
-            if (preg_match("/^".$pattern."\/energie\//",$_SERVER["PHP_SELF"]))
+            $path = DOL_URL_ROOT.'/energie/';
+            if (preg_match('/^'.preg_quote($path,'/').'/i',$_SERVER["PHP_SELF"]))
             {
                 $class='class="tmenusel"'; $_SESSION['idmenu']='';
             }
diff --git a/htdocs/lib/functions2.lib.php b/htdocs/lib/functions2.lib.php
index fe5b5951d5af66c10609bf3964ab5f4d3445839d..70862e6237dfb86da071e04248216798a87b49ba 100644
--- a/htdocs/lib/functions2.lib.php
+++ b/htdocs/lib/functions2.lib.php
@@ -300,12 +300,9 @@ function clean_url($url,$http=1)
 				$newproto = '';
 			}
 		}
-
-		// Add backslashes for regular expression
-		$proto = str_replace('/','\/',$proto);
 		
 		// On passe le nom de domaine en minuscule
-		$CleanUrl = preg_replace('/^'.$proto.$domain.'/i', $newproto.strtolower($domain), $url);
+		$CleanUrl = preg_replace('/^'.preg_quote($proto.$domain,'/').'/i', $newproto.strtolower($domain), $url);
 
 		return $CleanUrl;
 	}
diff --git a/htdocs/product.class.php b/htdocs/product.class.php
index 5c42c755401550595490725244cbf4227e3bf51b..1f73c8df6b5d484a8e1ac6c267867d07cd7d94e7 100644
--- a/htdocs/product.class.php
+++ b/htdocs/product.class.php
@@ -2546,8 +2546,7 @@ class Product extends CommonObject
 	{
 		$dir = dirname($file).'/'; // Chemin du dossier contenant l'image d'origine
 		$dirthumb = $dir.'/thumbs/'; // Chemin du dossier contenant la vignette
-		$pattern = str_replace('/','\/',$dir); // Add backslashes for regular expression
-		$filename = preg_replace('/'.$pattern.'/i','',$file); // Nom du fichier
+		$filename = preg_replace('/'.preg_quote($dir,'/').'/i','',$file); // Nom du fichier
 
 		// On efface l'image d'origine
 		dol_delete_file($file);