thangngoc89
thangngoc89

Reputation: 1400

How to replace with preg_replace php

I'm having an php array like this :

$_0xb29b = ['item1','item2','item3'];

And I have a text file like this

_0xb29b[0] foo foo foo foo foo foo _0xb29b[2]

Can You guys show me how to replace _0xb29b[0] in the text file with the right item in the array? I want the text tho be like this:

item1 foo foo foo foo foo foo item3

Upvotes: 0

Views: 91

Answers (2)

nwolybug
nwolybug

Reputation: 472

I spent too much time getting this to work not to post it. Doesn't use preg_match, but pretty much replicates it. First it creates the needle from the variable name. Then, searches for the needles in the haystack using substr_count and strpos. Then uses the positions of the found needles and length of the needle to grab the index of the variable and replace using the array of the variable used to create the needle. Links to all the sources at the bottom.

<?php

function print_var_name($var) {
    foreach($GLOBALS as $var_name => $value) {
        if ($value === $var) {
            return $var_name;
        }
    }
    return false;
}

$_0xb29b = array('item1','item2','item3');

$needle = print_var_name($_0xb29b);
$needle_length = strlen($needle);
$haystack = '_0xb29b[0] foo foo foo foo foo foo _0xb29b[2]';
$haystack_height = strlen($haystack);

$num_needles = substr_count($haystack,$needle) . '<br />';
if($num_needles>0){
    $offset = 0;
    for($i=0;$i<$num_needles;$i++){
        $needle_pos[$i] = strpos($haystack,$needle,$offset);
        $needle_index[$i] = substr($haystack,$needle_pos[$i]+$needle_length+1,1);
        if($needle_pos[$i]+$needle_length+3<$haystack_height){
            $haystack = substr($haystack,0,$needle_pos[$i]). ' ' .${$needle}[$needle_index[$i]] . ' ' . substr($haystack,$needle_pos[$i]+$needle_length+3);
        } else {
            $haystack = substr($haystack,0,$needle_pos[$i]). ' ' .${$needle}[$needle_index[$i]];
        }
        $offset = $needle_pos[$i]+1;
    }
}
echo $haystack;
?>

[Variables variables][1] used to turn the string back into a variable with the issue of the variable being an array and using

${$needle}[index] 

to call the array index.
http://php.net/manual/en/language.variables.variable.php
http://php.net/manual/en/function.substr.php
http://php.net/manual/en/function.strpos.php
http://php.net/manual/en/function.substr-count.php
How to get a variable name as a string in PHP?

Upvotes: 0

BlitZ
BlitZ

Reputation: 12168

Use preg_replace_callback():

<?php
// header('Content-Type: text/plain; charset=utf-8');

$str     = '_0xb29b[0] foo foo foo foo foo foo _0xb29b[2], _0xb29b[xxx]';
$_0xb29b = ['item1','item2','item3', 'xxx' => 5];

$result  = preg_replace_callback(
    '/\_0xb29b\[([^\]]+)\]/',
    function($matches)use($_0xb29b){
        return $_0xb29b[$matches[1]];
    },
    $str
);

echo $result;
?>

Shows:

item1 foo foo foo foo foo foo item3, 5

NOTE: To get file content as a string I suggest you to read manual on file_get_contents().

Upvotes: 2

Related Questions