Home > Mobile >  PHP: If a string begins with "20 ", "22 ", "24 "or "28 " how
PHP: If a string begins with "20 ", "22 ", "24 "or "28 " how

Time:01-25

i have for example the following strings:

"20 Secured file A"
"20 Secured 20 files: A, B, ...."
"22 Deleted file A"
"22 Deleted 22 files: A, B, ...."
"24 Moved file A"
"24 Moved 24 files: A, B, ...."
"28 Corrupt file A"
"28 Corrupt 28 files: A, B, ...."
"All correct 20 files: A, B, ...."
"All correct 22 files: A, B, ...."
"All correct 24 files: A, B, ...."
"All correct 28 files: A, B, ...."

Which function in PHP i can use to search for exact "20 " and cut it? All functions i tested only let me search for "20" and not "20 " so the 2nd case cut 2x "20" and not the first "20 ".

I only need a code that can cut the first three letters but only if they begins with "20 ", "22 ", "24 "or "28 "

Thank you very much :)

str_replace('24 ', '', $text);

CodePudding user response:

You can use preg_replace function for that:

preg_replace("/^2[0248] /", '', $string);
  •  Tags:  
  • php
  • Related