This question has already been answered:

From this text "[id120080895 | Lesch, hello!" I do such a "Lesha, hello!" The following code:

$txt = $result['text']; $arrgumentstxt = array('[',']','|','id',1,2,3,4,5,6,7,8,9,0); $formattxt = str_ireplace($arrgumentstxt, '', $txt); echo $formattxt; 

However, if the source text is in this format "[id120080895 | Lesha], hello! Here is his number +79789879878" , then the output is truncated number , and any other numbers.

How to cut exactly [id120080895 | Lesha], leave the name, but do not touch anything else, for example, if there are numbers in the text.

Reported as a duplicate by members of Kosta B. , 0xdb , Jarvis_J , Edward , Community Spirit Jul 15 '18 at 15:42 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • one
    @KostaB. a duplicate duplicate, but this question is asked because The decision made there does not work in certain cases. The author really should pay attention to the second answer to that question. - teran

1 answer 1

The second answer will suit you: https://ru.stackoverflow.com/a/851287/302920

 $string = '[id120080895|Лёша], привет! Вот его номер +79789879878'; echo preg_replace('/\[id.*?\|(.*?)\]/', "$1", $string);