1

here is my column title

132 lorem ipsum...
21-2 gold sky...
325/1 blue river...
420 health and food
right decision
... and so on.

So many titles begins with various integers and sometimes the integer is divided by / or -;

How can I trim all of them and get only alphabet part, i.e.:

lorem ipsum...
gold sky...
blue river...
health and food
right decision

Thanks.

Mohammad
  • 21,175
  • 15
  • 55
  • 84

2 Answers2

1

We can try using preg_replace here to remove initials numbers, forward slash, or dash, followed by optional whitespace:

$input = "132 lorem ipsum...\n21-2 gold sky...\n325/1 blue river...\n100 420 health and food\nright decision";
$output = preg_replace("/(?<=^|\n)[0-9\/ -]+/", "", $input);
echo $output;

lorem ipsum...
gold sky...
blue river...
health and food
right decision

Demo

Note this answer is robust to a line having more than one term containing numbers, e.g. 100 420 health and food becomes health and food.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 1
    I think you forgot a `^`. Though really this should be closed as too broad. No effort was shown by OP. – miken32 Oct 10 '18 at 14:36
  • @miken32 Actually, I was somewhat far off. We can use a lookbehind to check for either the start of the string, or a newline. If found, then we strip off numbers, slash, dash, or whitespace, in any amount. – Tim Biegeleisen Oct 10 '18 at 14:42
  • For this case [trim](http://php.net/manual/en/function.trim.php) is faster than preg_replace. – Marco Florian Oct 10 '18 at 17:20
-1

You can use preg_replace() with (^\d+) as pattern.

$str = "123 Lorem Ipsum";
$cleared = preg_replace("(^\d+)", "", $str);
echo $cleared; //will echo Lorem Ipsum
Hamza Abdaoui
  • 2,029
  • 4
  • 23
  • 36