View Single Post
Old 09-17-2002, 08:35 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
preg_replace regex question

i found a solution to my problem without having to create custom scripts .. but i'm still kinda curious what the preg_replace string would look like.

i have a text file named "sounds.txt"
Code:
1 Piano
2 Guitar
3 Organ
4 Steel Drums
The file has over 1000 sounds on it.

i wanted to extract only the sound names into an array, then bring them into a database. i thought the php function preg_replace would do this best, but i couldn't get it to work. here is how i attempted to do it:
PHP Code:
<?
// put each line of the text file into an array
$array=file("sounds.txt");

// strip out first number and space for every line
foreach($array as $each)
{
  
$newArray[]=preg_replace("^[0-9]*\s" " " $each);
}

// insert sounds in database
foreach($newArray as $each)
{
  
$result=mysql_query("insert into table set sound='$each'");
}

?>
now my question is really with the "preg_replace" function. is that regular expression correct for what i'm trying to do?

^[0-9]*\s

the above should match "1 " or "100 " :: note there is a space after the numbers.
sde is offline   Reply With Quote