How to extract data between strings

Started on the old forum in October 2011 · view the original in the archive · old links redirect here

6 replies · 381 views · started 15 years ago · 1 min read

4 participants
sladex Developer · 232 posts #1

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

$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.

sladex Developer · 232 posts #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:

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
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

P
P
P
P
P
etc..

Do anyone have solution for this? Thank You.

andrew Junior · 14 posts #5

sladex wrote: 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:

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
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

P
P
P
P
P
etc..

Do anyone have solution for this? Thank You.

  <?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); 
} 
?>
GumSlone Administrator Principal · 1,690 posts #6
preg_match('/[A-z]{1}-(.*)-[0-9]{1}\.[A-z]{3,4}/', $text, $matches);

for 65002-xh0dICiqQS and

preg_match('/-(\d+)-/', $text, $matches);

for 65002

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close