SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    find a number in a string

    Hi,

    I have a file path string and need to find the number in it:

    The file path for example is:

    content/pages/uploaded_images/10.jpg

    and I need to find 10.

    The file path will be the same although the ext will be jpg OR gif.

    Any help would be appreciated.

    Thanks!

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $filepath 'content/pages/uploaded_images/10.jpg';
    $fileText pathinfo($filepath,PATHINFO_FILENAME); 
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is for a path name in a string. I think Preg_match is what is needed and I am not sure what to use.

  4. #4
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cooldudekyle View Post
    This is for a path name in a string. I think Preg_match is what is needed and I am not sure what to use.
    The upper example gets the filename without excentsion in this case '10'. So it wont work if you have filenames like 'abcd10.jpg' and you need to get the number in the end of the file. If this is not the case I dont understand why would you need preg_match there, because preg_match takes also the argument in string format. So it makes no difference.

    Most simply this will work with pregmatch even if you have other characters in filename, but DON'T have any other numbers in the path or name.
    PHP Code:
    preg_match('/\d+/'$filePath$match); 
    I think you really need to be more specific what you want if you are looking for a specific answer.

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's solved it! Thanks - that what I wanted.

    Cheers!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •