Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 11-15-2009, 04:07 PM string work
Extreme Talker

Posts: 176
Trades: 0
I'm trying to get information from filenames.
It loops through everything in the directory, now I just need it to chop them up.

The format:
number-name.ext

I need $number to equal everything before the first dash, and $name to equal everything after the dash until the last period.

I've been trying a bunch of functions, but I can't get it quite right.

current code:
PHP Code:
$handle opendir('scans/3/');
while (
false !== ($file readdir($handle))){
  
$extplace strchr($file".");
  
$number substr($file,0,strpos($file"-"));
  
$name strchr($file"-");
  echo 
$number.'/'.$name.'<br />';

Currently it get's the number correctly, I just don't know how to do the name.

echo will be something like
10/-name.jpg


EDIT:
nvm, I'm pretty sure this worked
PHP Code:
  $name strchr($file"-");
  
$name substr($name1strlen($name)-5); 

Last edited by Skeddles; 11-15-2009 at 04:44 PM..
Skeddles is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-15-2009, 04:48 PM Re: string work
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
Trades: 0
preg_match('/^([0-9]+)-([^.]+)/', $file, $match);

The above will match the number and name (without the extention):

$number = $match[0];
$name = $match[1];
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 11-15-2009, 04:52 PM Re: string work
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Why don't you split on the '/' then ?
First, get the extension, then remove it from the string, and then split:
PHP Code:
<?php
$handle 
opendir('scans/');
while (
false !== ($file readdir($handle))){
  if((
$file!=='.') && ($file!=='..')){
    
$ext pathinfo($filePATHINFO_EXTENSION);
    
$clean=str_ireplace(".$ext"""$file);
    
$ary=explode("-"$clean);
    if(
sizeof($ary)==1){
      
$name=$ary[0];
      
$nbr="no number in this filename";
    }
    else{
      
$name=$ary[1];
      
$nbr=$ary[0];
    }
    print <<<txt
FS filename: $file
name is 
$name, number is $nbr
------------------------------

txt;
  }
}
My results are:
Quote:
tmo@NAS:/home/tmo$ php 1.php
FS filename: 4564563-sdkfjghsdlgjk.sdfdfg
name is sdkfjghsdlgjk, number is 4564563
------------------------------
FS filename: blah.jpg
name is blah, number is no number in this filename
------------------------------
FS filename: 12-lasdf.jpg
name is lasdf, number is 12
------------------------------
FS filename: 11-lo.jpg
name is lo, number is 11
------------------------------
tmo@NAS:/home/tmo$ ls -la scans/
total 16
drwxr-xr-x 2 tmo webdev 4096 Nov 15 21:51 .
drwxr-xr-x 130 tmo webdev 12288 Nov 15 21:42 ..
-rw-r--r-- 1 tmo webdev 0 Nov 15 21:44 11-lo.jpg
-rw-r--r-- 1 tmo webdev 0 Nov 15 21:44 12-lasdf.jpg
-rw-r--r-- 1 tmo webdev 0 Nov 15 21:51 4564563-sdkfjghsdlgjk.sdfdfg
-rw-r--r-- 1 tmo webdev 0 Nov 15 21:43 blah.jpg
tmo@NAS:/home/tmo$
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 11-19-2009, 09:27 AM Re: string work
Super Talker

Posts: 139
Name: John Davis
Trades: 0
Quote:
Originally Posted by mgraphic View Post
preg_match('/^([0-9]+)-([^.]+)/', $file, $match);

The above will match the number and name (without the extention):

$number = $match[0];
$name = $match[1];
Little correction:
$number = $match[1];
$name = $match[2];

$match[0] - contains all string under pattern
__________________
»
Please login or register to view this content. Registration is FREE
- Interactive maps for websites
»
Please login or register to view this content. Registration is FREE
for web developers
MapMaster is offline
Reply With Quote
View Public Profile Visit MapMaster's homepage!
 
Old 11-19-2009, 11:41 PM Re: string work
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
Trades: 0
Quote:
Originally Posted by MapMaster View Post
Little correction:
$number = $match[1];
$name = $match[2];

$match[0] - contains all string under pattern
Yes, you are right! Thanks for the correction.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to string work
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.70362 seconds with 12 queries