O
O
Oleg2017-05-29 07:46:38
PHP
Oleg, 2017-05-29 07:46:38

How to remove characters from the end of a string with 'substr'?

text text text 'what you need' text text
You also need to take into account what is required, it will constantly change, i.e. there will be a different number of characters.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rsa97, 2017-05-29
@Rsa97

Find the position of 'what you need'.
Find the length of 'what you need'.
Cut out 'what you want' with substr.

S
Sergey Sokolov, 2017-05-29
@sergiks

function rtrim( src, core) {
  var pos = src.indexOf(core);
  if( !!~pos) return src.substr(0, pos + core.length);
  else return src;
}

rtrim( "текст текст текст 'то что нужно' текст текст", "то что нужно") // текст текст текст 'то что нужно

V
Vlad, 2017-05-29
@DaFive

I don't know why substr is here.

var t = 'текст текст текст то что нужно  213 213 12 321текст текст';
console.log(new RegExp('то что нужно', 'ig').exec(t)[0]);

Well, or replace everything that is not needed with an empty string.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question