There is a string

Текст >>123 текст и ещё текст 

How to select >> 123 from here (after the arrows are only numbers) and replace for example with <a href=\"#"> here what is highlighted </a>

So I tried to do:

 preg_replace("/&gt;&gt;([0-9])<br \/>/Usi","<a href=\"#\\1\">>>\\1</a><br \/>",$str) 
  • What attempts to solve the problem were made by you? - lolbas
  • Now I use this option. But it only works if after the line there is a transfer preg_replace ("/ & gt; & gt; ([0-9]) <br \/> / Usi", "<a href=\"#\\1\"> >> \ \ 1 </a> <br \/> ", $ str) - WaQum
  • The code provided in the comment is completely unreadable. Paste it in the post - lolbas
  • Everything is ready 1234567 - WaQum
  • Why in a regular expression <br \/> ? It is because of this that “it only works if there is a transfer after the line” - lolbas

1 answer 1

Hmm, right ?

 <?php $str = 'Текст >>123 текст и ещё текст'; // Тут неясность - вставить после # найденное или нет. В запросе - нет, в попытке решения - есть. Тут вставляется, если не надо, убери первый \\1 $newStr = preg_replace('/>>(\d+)/i', '<a href="#\\1">>>\\1</a>', $str); print $newStr; 
  • Yes, everything is great. I already knew where there was a mistake. In the request, I indicated that it would be easier to explain. - WaQum