Questions tagged [preg-replace-callback]

preg_replace_callback() is a PHP function that uses regular expressions to find substrings and a PHP callback function to perform the replacement.

preg_replace_callback replaces the older (and insecure) /e flag in preg_replace, where PHP would eval the replace string and execute the PHP contained inside. preg_replace_callback uses a direct function call instead, which negates that risk entirely.

The most common use is where you need to use a regular expression to find strings, but you need a PHP function to transform that string. Consider the below function, which finds instances of Bob and makes them all uppercase. This example passes an anonymous function (PHP >= 5.3) but you can pass the function name instead.

echo preg_replace_callback('/Bob/', function($match) {
    return strtoupper($match[0]);
}, 'We like Bob');
// outputs We like BOB
412 questions
3
votes
2 answers

How to CamelCase an string value even if string separator is null?

I need to convert a given string value (with or without separator) into a CamelCase string. So far this is what I am doing: class UtilString { public function stringToCamelCase($string, $character = null) { return…
ReynierPM
  • 17,594
  • 53
  • 193
  • 363
3
votes
1 answer

The /e modifier is deprecated, use preg_replace_callback instead after migrating from php5.2.6

I recently migrated from php5.2.6 to php5.6.22 and now I am getting this error. Unkwown error. 8192: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead Seems that preg_replace is deprecated in…
3
votes
1 answer

preg_replace The /e modifier is deprecated using arrays as pattern and replacement

I used to have this block of code to simulate some sort of BBCode: $pattern = array( '/\\n/', '/\\r/', '/\[list\](.*?)\[\/list\]/ise', '/\[b\](.*?)\[\/b\]/is', '/\[strong\](.*?)\[\/strong\]/is', '/\[i\](.*?)\[\/i\]/is', …
Lucas
  • 383
  • 1
  • 5
  • 17
3
votes
2 answers

Replace string within parenthesis with column data

I have written some code to allow calculations to be done on specific columns of data. For example {1}*{2} would result in column 1 being multiplied by column 2. What I need to be able to do is replace these numbers with the actual values of the…
3
votes
3 answers

PHP Understanding callbacks - difference between preg_replace_callback() and preg_match_all()

I am a bit confused on the use of preg_replace_callback() I have a $content with some URLs inside . Previously I used $content = preg_match_all( '/(http[s]?:[^\s]*)/i', $content, $links ); foreach ($links[1] as $link ) { // we have…
Obmerk Kronen
  • 15,619
  • 16
  • 66
  • 105
2
votes
1 answer

PHP PREG_REPLACE REGEX Mention Username

user_model.php class User_model extends CI_Model{ function get_fullname_by_username($username){ $query=$this->db->select('user_first_name,user_last_name')->where('user_name',$username)->get('user'); if($query->num_rows()==0){ …
Bias Tegaralaga
  • 2,240
  • 5
  • 21
  • 26
2
votes
3 answers

Parse bbcode youtube tag in text and replace with html including the video id

I'm building a blog that should parse bbcode tags like this: Input: Output:
bloudermilk
  • 17,820
  • 15
  • 68
  • 98
2
votes
1 answer

php preg_replace word to link, but word can't be already as part of anchor

I have some HTML content:

Nunc nulla tincidunt metus sed eros auctor sagittis. Sed malesuada, mi et ornare molestie, urna dui condimentum nulla, ut luctus nisl ipsum ut dui. Praesent…

Luc
  • 51
  • 3
2
votes
2 answers

Using preg_replace_callback with external class

I have a question for you! Normally, if you call a callback function within an OOP context you have to use array(&$this, 'callback_function') That's what I figured out. But now I want to call a callback in an external class, due to having to much…
Stephan
  • 327
  • 3
  • 11
2
votes
1 answer

Is there a PHP port of Java's Matcher class?

I'm porting YUI's CssCompressor, which has several Matcher uses, to PHP. For the sake of long-term maintenance I'd like to keep the PHP port as similar to the Java original as possible (preg_replace_callback of course works, but drastically changes…
Steve Clay
  • 8,671
  • 2
  • 42
  • 48
2
votes
3 answers

Making a simple templating engine in PHP

I need to write a simple PHP function to replace text between {{ }} characters with their respective data. Example: String: "and with strange aeons even {{noun}} may {{verb}}" $data = ['noun' => 'bird', 'verb' => 'fly']; Result: "and with strange…
yevg
  • 1,846
  • 9
  • 34
  • 70
2
votes
1 answer

Using preg_replace_callback to find and replace a function signature with a variable number of parameters

Using the PHP preg_replace_callback function, I want to replace the occurrences of the pattern "function([x1,x2])" with substring "X1X2", "function([x1,x2,x3])" with substring "X1X2X3", and in general: "function([x1,x2,...,x2])" with substring…
user10906061
2
votes
1 answer

Replace a string that matches a pattern using preg_replace_callback

I want to replace the occurrences of the pattern "binary_function([x,y])" with substring "XY" in a given string. I have it working with the following code: // $string is the string to be searched $string =…
user10906061
2
votes
2 answers

Replace odd positions in string with a random digit in php

I have a string from which I want to replace only the odd positions by a random digit. For Example, the string is '123456'. Now the output I want is '528496'; Note that the digits 1,3,5 in odd positions are replaced by random digits 5,8,9. I know…
user6379404
2
votes
0 answers

How to fix this regex for PHP 7.2 to avoid PREG_JIT_STACKLIMIT_ERROR?

I am moving some code (that I inherited) from PHP 5.4 to PHP 7.2. The following preg_replace_callback fails with preg_last_error of PREG_JIT_STACKLIMIT_ERROR, and returns an empty string. This happens when the subject string gets beyond a certain…
S. Arquet
  • 29
  • 4
1 2
3
27 28