How to extract data between strings

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to extract data between strings

    Hi,
    Can you help me with that.
    I'm tried few times, but failed.
    I want to extract numbers from the name of file.

    File name':P-65002-xh0dICiqQS-1.jpg

    How to get these numbers between P- and - ?

    I tried like that:

    PHP Code:
    <?php

    $text 
    'P-65002-xh0dICiqQS-1.jpg'
    preg_match("P-(.*)-"$text$matches); 
    $value $matches[1]; 

    echo
    "$value";
     
    ?>
    But I'm sure that part: preg_match("P-(.*)-" has to be changed.
    I don't know parrameter that should go there.


    Can anyone help me with that?
    Thank You very much.
    OR JUST SEND PM

    #2
    PHP Code:
    <?php
    $a
    =explode("-",$text);
    echo 
    $a[1];
    ?>

    Comment


      #3
      Originally posted by ionutvmi View Post
      <?php
      $a=explode("-",$text);
      echo $a[1];
      ?>
      Damn, working, thank you very much.
      OR JUST SEND PM

      Comment


        #4
        Hmm...it's working for one file, but when I have all list of files, its not working.
        Let's say I have list of files:
        Code:
        P-100208-UpYdMeMtcm-1.jpg
        P-100216-4MwGRw5swv-1.jpg
        P-100217-suHqW1kFOf-1.jpg
        P-100218-wsgX3Y8v77-1.jpg
        P-100219-Feclv8jfVk-1.jpg
        P-100220-Pc6D8F7JNd-1.jpg
        P-10054-2zWg4pJOid-1.jpg
        P-100896-LbV5S2p98S-1.jpg
        P-100897-02YW6j9jst-1.jpg
        P-100899-VxvwjVdxbW-1.jpg
        P-100901-3oICxPu8Ul-1.jpg
        P-100902-hR3wqSFoC4-1.jpg
        P-100908-aICRXta8R2-1.jpg
        P-100910-xKo8CiuVuv-1.jpg
        P-100912-9M4YoqvxNX-1.jpg
        P-100914-gxT7upzalv-1.jpg
        P-100918-wPkDryqSjm-1.jpg
        etc...
        And if I using that code:
        PHP Code:
          <?php
        if ($handle opendir('games/screen/4/')) {
            while (
        false !== ($file readdir($handle))) {
                if (
        $file != "." && $file != "..") {
                
        $wordChunksLimited explode("-"$file);
                    echo 
        "$wordChunksLimited[0]\n";
                }
            }
            
        closedir($handle);
        }
        ?>
        Then Is exploding only
        Code:
        P
        P
        P
        P
        P
        etc..
        Do anyone have solution for this?
        Thank You.
        OR JUST SEND PM

        Comment


          #5
          Originally posted by sladex View Post
          Hmm...it's working for one file, but when I have all list of files, its not working.
          Let's say I have list of files:
          Code:
          P-100208-UpYdMeMtcm-1.jpg
          P-100216-4MwGRw5swv-1.jpg
          P-100217-suHqW1kFOf-1.jpg
          P-100218-wsgX3Y8v77-1.jpg
          P-100219-Feclv8jfVk-1.jpg
          P-100220-Pc6D8F7JNd-1.jpg
          P-10054-2zWg4pJOid-1.jpg
          P-100896-LbV5S2p98S-1.jpg
          P-100897-02YW6j9jst-1.jpg
          P-100899-VxvwjVdxbW-1.jpg
          P-100901-3oICxPu8Ul-1.jpg
          P-100902-hR3wqSFoC4-1.jpg
          P-100908-aICRXta8R2-1.jpg
          P-100910-xKo8CiuVuv-1.jpg
          P-100912-9M4YoqvxNX-1.jpg
          P-100914-gxT7upzalv-1.jpg
          P-100918-wPkDryqSjm-1.jpg
          etc...
          And if I using that code:
          PHP Code:
            <?php
          if ($handle opendir('games/screen/4/')) {
              while (
          false !== ($file readdir($handle))) {
                  if (
          $file != "." && $file != "..") {
                  
          $wordChunksLimited explode("-"$file);
                      echo 
          "$wordChunksLimited[0]\n";
                  }
              }
              
          closedir($handle);
          }
          ?>
          Then Is exploding only
          Code:
          P
          P
          P
          P
          P
          etc..
          Do anyone have solution for this?
          Thank You.
          PHP Code:
            <?php 
          //assuming you have "P-100208-UpYdMeMtcm-1.jpg" in that directory,
          if ($handle opendir('games/screen/4/')) { 
              while (
          false !== ($file readdir($handle))) { 
                  if (
          $file != "." && $file != "..") { 
                  
          $wordChunksLimited explode("-"$file); 
                      echo 
          $wordChunksLimited[2]."\n"// $wordChunksLimited[0] would be "P", $wordChunksLimited[1] would be "100208", $wordChunksLimited[2] would be "UpYdMeMtcm" and $wordChunksLimited[3] would be "1"... explode function returns an array
                  

              } 
              
          closedir($handle); 

          ?>

          Comment


            #6
            PHP Code:
            preg_match('/[A-z]{1}-(.*)-[0-9]{1}\.[A-z]{3,4}/'$text$matches); 
            for 65002-xh0dICiqQS
            and
            PHP Code:
            preg_match('/-(\d+)-/'$text$matches); 
            for 65002
            Advertise your mobile site for FREE with AdTwirl

            Comment


              #7
              Working just great. Thank You very much.
              OR JUST SEND PM

              Comment

              Working...
              X