6

Is it possible use str_replace() and use function in replace?

$value = "gal($data)";

$replace = str_replace($dat, $value, $string);

gal($data) is a function and I need replace one code for this function and show, but the script only give me finally this gal($data), and the function no show nothing

Is it possible use str_replace() for replace code and replace by the function or some similar method?

7ochem
  • 2,183
  • 1
  • 34
  • 42
Robert
  • 315
  • 2
  • 5
  • 9

1 Answers1

11

PHP has a function called preg_replace_callback that does this. When you pass it a callback function, it will pass each match through your function. You can choose to replace, based upon the matched value, or ignore it.

As an example, suppose I have a pattern that matches various strings, such as [a-z]+. I may not want to replace every instance with the same value, so I can call a function upon eat match found, and determine how I ought to respond:

function callback ($match) {
    if ($match[0] === "Jonathan")
        return "Superman";
    return $match[0];
}

$subject = "This is about Jonathan.";
$pattern = "/[a-z]+/i";
$results = preg_replace_callback($pattern, "callback", $subject);

// This is about Superman.
echo $results;

Note in our callback function how I am able to return special values for certain matches, and not all matches.

Expanding Abbreviations

Another example would be a lookup. Suppose we wanted to find abbreviations of programming languages, and replace them with their full titles. We may have an array that has abbreviations as keys, with long-names as values. We could then use our callback ability to lookup the full-length names:

function lookup ($match) {
    $langs = Array(
        "JS"  => "JavaScript", 
        "CSS" => "Cascading Style Sheets", 
        "JSP" => "Java Server Pages"
    );
    return $langs[$match[0]] ?: $match[0];
}

$subject = "Does anybody know JS? Or CSS maybe? What about PHP?";
$pattern = "/(js|css|jsp)/i";
$results = preg_replace_callback($pattern, "lookup", $subject);

// Does anybody know JavaScript? Or Cascading Style Sheets maybe? What about PHP?
echo $results;

So every time our regular expression finds a match, it passes the match through lookup, and we can return the appropriate value, or the original value.

Community
  • 1
  • 1
Sampson
  • 265,109
  • 74
  • 539
  • 565
  • I don´t know that function you can tell me how use it in this case , thank´s for your help – Robert Nov 27 '12 at 22:46
  • @user1851816 Perhaps I'm confused about what it is you're asking. The function I suggested will let you perform string replacement via a callable function. This lets you add a bit more logic into how you will replace, when you will replace, and with what you will replace as opposed to straight-forward 1:1 string replacement. – Sampson Nov 27 '12 at 22:48
  • It´s simple if you can tell me how modificate the code perfect i try this 3 days ago and no get this point for works fine , thank´s for all – Robert Nov 27 '12 at 22:53
  • 1
    Thank´s foor your help , i understand how works the function now , thank´s for all and for your time , regards ! – Robert Nov 27 '12 at 23:05