You need to create a regular expression to search for the following template:

>>какие-то_числа_без_пробелов, текст 

and replace it with

 <a href="#какие-то_числа_без_пробелов">какие-то_числа_без_пробелов</a>, текст 

example:

 >>12344, log success <a href="#12344">12344</a>, log success 

These regular expressions do not go at all, or it may take bad stuff.

  • And for this case, "1 text 2 3", the link to each number should be? - jekaby
  • And the number only at the beginning of the line can be? - jekaby
  • regular-expressions see label description. There is good material on rehexam. - ReinRaus

3 answers 3

 $s = "12345, текст"; $s = preg_replace('/^(\d+)(.*)$/', '<a href="#$1">$1</a>$2', $s); var_dump($s); 

Conclusion

 string '<a href="#12345">12345</a>, текст' (length=38) 

    possible without regulars

     $string = explode(',',$string); echo '<a href="'.$string[0].'" >'.$string[0].'</a>'; 

    where it is so divided by , the first (zero) element of the array gets a number, and we derive it as we want.

      This option still looks that the number is separately worth (Tipo 2x4):

       $s = "123, текст 1 2x4"; $s = preg_replace('/\b(\d+)\b/', '<a href="#$1">$1</a>$2', $s); var_dump($s); // string(56) "<a href="#123">123</a>, текст <a href="#1">1</a> 2x4"