Frage

i have a problem i want to add slashes at the starting and the end of each string of my array.

This is an example of my actual array :

$patte = array();
$patte[0] = "httpd";
$patte[1] = "vsftpd";
$patte[2] = 'gohphp';
$patte[3] = 'abcdef';

i use this array for taking information into a DataBase so i can't place slashes now, or this is going to not working.

(mysql_query ... while mysql_fetch_array ...)

I need to rename these entry. For this i use a second array, and with the command : "preg_replace" i can translate every strings like i want. But preg_replace want me to add slashes in $patte

I want to obtain an array like this

$pattes = array();
$pattes[0] = "/httpd/";
$pattes[1] = "/vsftpd/";
$pattes[2] = '/gohphp/';
$pattes[3] = '/abcdef/';

Can you help me please. I'm gonna have like 1000 line into this array.

War es hilfreich?

Lösung

Using array_map() you can apply callback to every element of your array :

function addSlashes($str)
{
    return "/".$str."/";
}
$newArray = array_map("addSlashes", $patte);//array with the new values

Andere Tipps

Use array_map:

$pattes = array_map(function($str) {
  return '/'.$str.'/';
}, $pattes);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top