Remove numeric prefix from string - PHP regex

Would anyone want to help me with a regex to reliably recognize and remove any number followed by a dot at the beginning of a line? So that

1. Introduction

becomes

Introduction

and

1290394958595. Appendix A

becomes

Appendix A
+3
source share
9 answers

Try:

preg_replace('/^[0-9]+\. +/', '', $string);

What gives:

php > print_r(preg_replace('/^[0-9]+\. +/', '', '1231241. dfg'));
dfg
+6
source

I know the question is closed, only my two cents:

preg_replace("/^[0-9\\.\\s]+/", "", "1234. Appendix A");

It’s best to work, in my opinion, mainly because it will also handle cases like

1.2 This is a level-two heading
+7
source

:

^[0-9]+\.
+3

, , , , . , .

// remove everything before first space
echo trim(strstr('1290394958595. Appendix A', ' '));

// remove all numbers and dot and space from the left side of string
echo ltrim('1290394958595. Appendix A', '0123456789. ');

, .

+1

$string = "1290394958595. Appendix A";
$first_space_position = strpos(" ", $string);
$stuff_after_space = substr($string, $first_space_position);

$string = "1290394958595. Appendix A";
$first_dot_position = strpos(".", $string);
$stuff_after_dot = substr($string, $first_dot_position);
0

:

/[0-9]+\./

Btw, I would definitely use regex here, since they are very reliable and light.

Greetings

0
source
print preg_replace('%^(\d+\. )%', '', '1290394958595. Appendix A');
0
source

PHP function to find a string for regular expression and replace it with something else preg_replace. In this case, you want something like:

$mytitle = preg_replace('/^[0-9]+\. */', '', $myline);
0
source
$str="1290394958595. Appendix A";
$s = explode(" ",$str,2);
if( $s[0] + 0 == $s[0]){
  print $s[1];
}
0
source

Source: https://habr.com/ru/post/1735835/


All Articles