Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 09-17-2002, 08:35 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
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
Old 09-18-2002, 08:02 AM   #2 (permalink)
sdeming
Code Monkey
 
Join Date: Jul 2002
Location: Michigan
Posts: 85
sdeming is on a distinguished road
preg_replace is probably overkill for extracting such uniform fields, so I would recommend using something like this:
PHP Code:
$newArray[]=substr(strstr($each' '), 1); 
Or using explode:
PHP Code:
$parts explode(" "$each);
$newArray[] = $parts[1]; 
__________________
Scott
B4 09 BA 09 01 CD 21 CD 20 53 63 6F 74 74 24
sdeming is offline   Reply With Quote
Old 09-18-2002, 08:43 AM   #3 (permalink)
Admin
$_['Your_Mom'];
 
Admin's Avatar
 
Join Date: May 2002
Location: Santee
Posts: 627
Admin is on a distinguished road
wouldnt the explode() command fail when there is a space in the sound name? ie. 'Steel Drums' so it would break it up into

number
word1
word2

and your result would only be 'word1' ... please correct me if i am wrong.....
__________________


Urban Clothing
Admin is offline   Reply With Quote
Old 09-18-2002, 08:48 AM   #4 (permalink)
sdeming
Code Monkey
 
Join Date: Jul 2002
Location: Michigan
Posts: 85
sdeming is on a distinguished road
Oops, you are right! Disregard the explode method and just use the substr(strstr(...)) one instead.
__________________
Scott
B4 09 BA 09 01 CD 21 CD 20 53 63 6F 74 74 24
sdeming is offline   Reply With Quote
Old 09-18-2002, 10:23 AM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
holy smokes batman! strstr() is a beatiful thing =)

thanks
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick question on <table>'s Redline HTML, XML, Javascript, AJAX 3 10-30-2004 08:12 PM
Question marks and apostrophes Belisarius HTML, XML, Javascript, AJAX 4 05-28-2004 04:11 PM
Compiler Question cheawick Standard C, C++ 3 04-30-2004 03:06 AM
DB Design Question Part II sde Program Design and Methods 3 05-10-2003 12:24 PM


All times are GMT -8. The time now is 07:26 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting