viktorino
viktorino

Reputation: 809

change content outside and inside brackets with preg_replace

i need to change this line: count(id)

to line like this count(tableName.id)

i try to do this with preg match and replace like this:

    $a = "count(id)";
    $regex = "/\w{3,}+\W/";
    $dd = preg_match("/\(.*?\)/", $a, $matches);
    $group = $matches[0];

    if (preg_match($regex, $a)) {

    $c = preg_replace("$group", "(table.`$group`)", $a);

    var_dump($c);

    }

output that i got is : count((table.(id))) its outputting me extra brackets . i know the problem but i can't find solution because my regex knowledge not so good.

Upvotes: 0

Views: 173

Answers (1)

Sabuj Hassan
Sabuj Hassan

Reputation: 39355

$a = "count(id)";
$regex = "/\w{3,}+\W/";
$dd = preg_match("/\((.*?)\)/", $a, $matches);

$group = $matches[1]; // <-- you'll get error if the above regex doesn't match!

if (preg_match($regex, $a)) {
    $c = preg_replace("/$group/", "table.$group", $a);
}

Upvotes: 1

Related Questions