Beny Bond Banjarnahor Beny Bond Banjarnahor - 11 months ago 105
MySQL Question

get string after blackslah in php

Here's my problem. I want to get

2017
from the string below. I've tried the code below but it doesn't work. I want to use regex but I don't understand the backslash.

Code :

$str = explode("\","File=D:\Shared\print\2017\september\26\printing\baryun - cetak transferpaper.tif");
echo $str[count($str)-5];


String :

File=D:\Shared\print\2017\september\26\printing\baryun - cetak transferpaper.tif

Answer Source

You can match it using ^File=D:\\[a-zA-Z]+\\[a-z]+\\([0-9]{4})

That is:

<?php
   $line = 'File=D:\Shared\print\2017\september\26\printing\baryun - cetak transferpaper.tif';
    $r = "/^File=D:\\\\[a-zA-Z]+\\\\[a-z]+\\\\([0-9]{4})\\\\([a-z]+)\\\\([0-9]{1,2})/";

   if (preg_match($r, $line, $match)) {
      print $match[1];
      }
?>

To match the year, month and day, use ^File=D:\\\\[a-zA-Z]+\\\\[a-z]+\\\\([0-9]{4})\\\\([a-z]+)\\\\([0-9]{1,2})

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download