W
W
Webber2020-05-25 14:10:32
PHP
Webber, 2020-05-25 14:10:32

How to remove part of a string as quickly as possible?

There is a line like:

$string = '40|https://site1.com/files/02bea218b601ef2cbc74e08dc8d78_1000_1000.png;77|https://site1.com/files/29037cbdc27707fe2d6b2cf4d3924_1000_1000.png;78|https://site1.com/files/923b6f214f8bffcf20fbbebe5365b_1000_1000.png;85|https://site1.com/files/f8b6dbe261e89877e23e6a6f00003_1000_1000.png';


How to quickly remove everything between the characters |; and get the result:
$result = '40,77,78,85'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VicTHOR, 2020-05-25
@AKLZephyr

$string = '40|https://site1.com/files/02bea218b601ef2cbc74e08dc8d78_1000_1000.png;77|https://site1.com/files/29037cbdc27707fe2d6b2cf4d3924_1000_1000.png;78|https://site1.com/files/923b6f214f8bffcf20fbbebe5365b_1000_1000.png;85|https://site1.com/files/f8b6dbe261e89877e23e6a6f00003_1000_1000.png';
$result = substr(preg_replace('/\|.*?(?:;|$)/', ',', $string), 0, -1);
echo $result; // 40,77,78,85

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question