3

I have issue with preg_replace function in PHP. I cant figure out a pattern and a replacement.

I have this two strings and some code:

    $dirname1 = 'hdadas/dasdad/dasd/period_1min';
    $dirname2 = 'hdadas/dasdad/dasd/period_1min/abcdrfg.php';

    $pieces1 = explode('/', $dirname1);
    $pieces2 = explode('/', $dirname2);

    $dirname1 = end($pieces1);  // output will be period_1min
    $dirname2 = end($pieces2); // output will be abcdrfg.php

    $output = preg_replace($pattern, $replacement, $dirname1); // or (..,..,$dirname2

    echo $output; // i need 1min(without period_) or abcdrfg (without .php)

UPD:

   function Cat($dirname)
   {
      $name = explode('/', $dirname);

      $pattern = ???;
      $replacement = ???;

      return preg_replace($pattern, $replacement, $dirname1);
    }

    print(Cat('hdadas/dasdad/dasd/period_1min'))); // output need 1min only
    print(Cat('hdadas/dasdad/dasd/period_1min/abcdrfg.php'))); // output  need abcdrfg only  
2
  • 1. The first 8 lines is the same as: $dirname1 = basename($dirname1); 2. Which pattern does this follow when you want period_ for the first and abcdrfg for the second ? Commented Mar 18, 2015 at 12:36
  • i cant figure out what to write in $pattern and $replacement Commented Mar 18, 2015 at 12:37

2 Answers 2

2

This should work for you:

(Here I use basename() to get only the last part of the path, then I use pathinfo() to get the last part without the extension. After this I just use preg_replace() to replace everything before the underscore with an empty string)

<?php

    $dirname1 = "hdadas/dasdad/dasd/period_1min";
    $dirname2 = "hdadas/dasdad/dasd/period_1min/abcdrfg.php";

    $dirname1 = pathinfo(basename($dirname1))["filename"];
    $dirname2 = pathinfo(basename($dirname2))["filename"];

    echo $output = preg_replace("/(.*)_/", "", $dirname1); 


?>

output:

1min
abcdrfg
Sign up to request clarification or add additional context in comments.

6 Comments

@Undertaker echo phpversion(); = ? <5.4 ?
Yes 5.3.3 that why :)
@Undertaker Maybe you want to upgrade your php version, we live in 2015, so you can use array dereferencing
I cant project run on < 5.4
@Undertaker Then you can change these lines from: $dirname1 = pathinfo(basename($dirname1))["filename"]; to this: $dirname1 = pathinfo(basename($dirname1)); $dirname1 = $dirname1["filename"]
|
1

How about this regex /^.+_|\.[^.]+$/:

$dirname1 = 'hdadas/dasdad/dasd/period_1min';
$dirname2 = 'hdadas/dasdad/dasd/period_1min/abcdrfg.php';

$pieces1 = explode('/', $dirname1);
$pieces2 = explode('/', $dirname2);

$dirname1 = end($pieces1);  // output will be period_1min
$dirname2 = end($pieces2); // output will be abcdrfg.php

$output = preg_replace('/^.+_|\.[^.]+$/', '', $dirname1); // or (..,..,$dirname2
echo $output,"\n"; // i need 1min(without period_) or abcdrfg (without .php)

$output = preg_replace('/^.+_|\.[^.]+$/', '', $dirname2); // or (..,..,$dirname2
echo $output,"\n"; // i need 1min(without period_) or abcdrfg (without .php)

Output:

1min
abcdrfg

7 Comments

Thank you, but it outputs abcdrfg.php (with .php) on version 5.3.3
@Undertaker: Sorry, typo I forget the + quantifier in the second example. See my edit, it works now.
How the regex look like if it will be period_week_sunday?
@Undertaker: what do you want to keep?
@Yes, you're right, change the the regex to: ^[^_]+_|\.[^.]+$
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.