Php 在找到的数字前添加文本,字符串操作

Php 在找到的数字前添加文本,字符串操作,php,Php,我正在用php解析包含大量数据的大文件。我被一个案子缠住了: 如果在我的字符串中有一个数字在花括号中,我需要先添加一些文本 示例: {4316} Test 应该是: {=ATTRVAL("4316")} Test Some random text {=ATTRVAL("2323")} and {=ATTRVAL("3232")} I got here. 或在字符串的中间: Some random text {2323} and {3232} I got here. 应该是: {=ATTR

我正在用php解析包含大量数据的大文件。我被一个案子缠住了:

如果在我的字符串中有一个数字在花括号中,我需要先添加一些文本

示例

{4316} Test
应该是:

{=ATTRVAL("4316")} Test
Some random text {=ATTRVAL("2323")} and {=ATTRVAL("3232")} I got here.

或在字符串的中间:

Some random text {2323} and {3232} I got here.
应该是:

{=ATTRVAL("4316")} Test
Some random text {=ATTRVAL("2323")} and {=ATTRVAL("3232")} I got here.
到目前为止,我尝试了很多字符串函数,但这次运气不好

public static function parseStringWithAttributeValue($attributeValue)
{
    preg_match_all('!\d+!', $attributeValue, $matches);

    $string = '';

    foreach ($matches as $match)
    {
       // string .= $match
    }

    return $string;
}
我试图先提取数字,然后创建新文本,但这是错误的逻辑。如果可能的话,理想的情况是用preg_替换,但我到目前为止没有运气。 我也试过Stru_替换,但我想我的知识只能做到这一点。 如果有人知道该采取什么方法,我很乐意得到任何建议。

试试这个:

public static function parseStringWithAttributeValue($attributeValue){
    return preg_replace('/{(\d+)}/i', '{=ATTRVAL("$1")}', $attributeValue);
}