Title: How to add a string to both sides of a found substring
Tags: php;regex
Question: I have an input string:

```10 birds have found 5 pears and 6 snakes.
```

How can I put html tags around substrings found with a regular expression? 

For instance, I want all numbers to be bold, like this:

``` <b&gt;10</b&gt; birds have found <b&gt;5</b&gt; pears and <b&gt;6</b&gt; snakes.
```

Comment: That's not difficult, have you tried anything?

Comment: preg_replace_callback()

Comment: @LarsStegelitz: this function is not needed `preg_replace` should suffice.

Comment: When you're asking for specific help with debugging something, you need to show what you've tried.

Comment: I have tried, still don't which PHP functions to use. To find the substring is not hard, to put string in front and behind is harder. I tried combination of preg_mach and str_pad() but it wasnt the best solution.

Here is the accepted answer: You can try use ```preg_replace()``` function:

```$str = '10 birds have found 5 pears and 6 snakes.';
echo preg_replace('/(\d+)/', '<b&gt;$1</b&gt;', $str);
```

Output:

```<b&gt;10</b&gt; birds have found <b&gt;5</b&gt; pears and <b&gt;6</b&gt; snakes.
```

Comment for this answer: For god sake. I forgot I can use the found string as variable. That was it. Thanks a lot.

Here is another answer: Without regex you can do that:

```$items = explode(' ', $text);
$result = array_reduce($items, function ($c, $i) {
    return ($c ? $c . ' ' : $c)
         . (is_numeric($i) ? '<b&gt;' . $i . '</b&gt;' : $i);
});
```

Comment for this answer: @PiotrOlaszewski: Because this way, when it's possible to use it, is 2x faster for this kind of string length.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:02/25/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><regex>', 'Title': 'How to add a string to both sides of a found substring', 'ViewCount': 94, 'id': 28727536, 'user_AboutMe': '<p>Programmer randomly bumping into challenge</p>\n', 'user_AccountId': 2203217.0, 'user_DisplayName': 'Croolman', 'user_Reputation': 1093.0, 'user_Views': 227.0, 'user_id': 1947081.0}}