Author Topic: preg_replace help  (Read 377 times)

0 Members and 1 Guest are viewing this topic.

Offline Swift-RTopic starter

  • Irregular
  • Posts: 5
    • View Profile
preg_replace help
« on: February 14, 2010, 11:04:59 AM »
Can someone explain what is wrong with this code?

$string "blabla <'bl'a'> hello";
$result preg_replace"#<'([^'>]\S+)'>#""'" $this->escape_string"$1" ) . "'"$string );


What this is supposed to do is to pick the text inside <' and '> and do a mysql_real_escape_string ($this->escape_string) to that text.
It is displaying the $string correctly, without the < and > but escape_string isn't working. It's supposed to return blabla 'bl\'a' hello instead of blabla 'bl'a' hello.

I have also tried to use the function strtoupper instead of $this->escape_string but it is still not working.

Any ideas?

Offline salathe

  • Lazy
  • Administrator
  • Addict
  • *
  • Posts: 1,540
  • Gender: Male
  • Temperament: Snuggly
    • View Profile
    • My Blog
Re: preg_replace help
« Reply #1 on: February 14, 2010, 11:36:48 AM »
You can't call preg_replace like that (and have it do what you're expecting). The replacement string will effectively be '$1' which of course just wraps the matched value in single quotes.

Instead, you should use a callback function with preg_replace_callback() like:

// The match gets fed into this function which returns the replacement string
public function escape_and_quote_match($match) {
	
return 
"'" $this->escape_string($match[1]) . "'";
}

// How to call preg_replace_callback
$result preg_replace("#<'([^'>]\S+)'>#", array($this'escape_and_quote_match'), $string);

PHP Documentation — Read it, or the badger will come for you.
http://php.net/manual/

Offline Swift-RTopic starter

  • Irregular
  • Posts: 5
    • View Profile
Re: preg_replace help
« Reply #2 on: February 14, 2010, 12:19:53 PM »
You can't call preg_replace like that (and have it do what you're expecting). The replacement string will effectively be '$1' which of course just wraps the matched value in single quotes.

Instead, you should use a callback function with preg_replace_callback() like:

// The match gets fed into this function which returns the replacement string
public function escape_and_quote_match($match) {
	
return 
"'" $this->escape_string($match[1]) . "'";
}

// How to call preg_replace_callback
$result preg_replace("#<'([^'>]\S+)'>#", array($this'escape_and_quote_match'), $string);


Thank you!

$result preg_replace_callback"#<'([^'>]\S+)'>#"create_function'$matches''return strtoupper($matches[1]);' ), $string);

But if I use $this->escape_string($matches[1]); it returns this error:

Using $this when not in object context in ... : runtime-created function on line 1

Guess I'll have to use your example.
« Last Edit: February 14, 2010, 12:20:38 PM by Swift-R »

Offline salathe

  • Lazy
  • Administrator
  • Addict
  • *
  • Posts: 1,540
  • Gender: Male
  • Temperament: Snuggly
    • View Profile
    • My Blog
Re: preg_replace help
« Reply #3 on: February 14, 2010, 12:40:24 PM »
That's correct, anonymous functions (those created using create_function) do not belong to a class/object so $this is meaningless in that case.
PHP Documentation — Read it, or the badger will come for you.
http://php.net/manual/