简体   繁体   中英

Php get string after x characters

I have an array which returns me paths like these:

C:\xampp\htdocs\app\storage\app/img/img24.png"  


C:\xampp\htdocs\app\storage\app/img/img3.png"

I need to extract the number from those parts, in this case 24 and 3 and save them into another array. So far I extracted the length like this:

$pathLen = strlen(storage_path('app/img/'));

That returned me 45, but I dont know how to continue

preg_match('~img/img(.*?).png~', $arrayinput, $output);

print_r($output[1]);

You can get substr from the last / , then use intval() to get the int. Live demo .

<?php
$string = "C:\xampp\htdocs\app\storage\app/img/img24.png";
echo intval(substr($string, strrpos($string, '/') +4));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM