S
S
Stanislav Shabalin2020-04-29 22:35:53
PHP
Stanislav Shabalin, 2020-04-29 22:35:53

How to remove a certain combination of characters from the end of a string?

Good evening.
I'm trying to figure out the preg_replace construct in order to remove a combination of characters and / or numbers at the end of the line, but I'm dumb and don't come out right away.
I thought so, but it doesn't work:

$image_name = preg_replace("/_int[-\d]$/",$image_name);


There are lines that always end with the word _int or _int-01 or _int01
Help with the expression to delete this expression, please.
And it would be great if you could also find out that there is such a replacement and return true or false. Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2020-04-29
@Starck43

$image_name = preg_replace("/_int-?\d*$/", "", $image_name, -1, $count);

$count will contain the number of replacements made
https://www.php.net/manual/en/function.preg-replace.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question