News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

Preg_replace

Started by Harzem, June 26, 2006, 06:22:16 PM

Previous topic - Next topic

Harzem

$data = preg_replace('~&#(\d{4,5}|[2-9]\d{2,4}|1[2-9]\d);~', '&#$1;', $data);

This is used in SMF a lot. But I don't know what it is for. Can somebody enlighten me a bit? :)

winrules

Quote from: HarzeM on June 26, 2006, 06:22:16 PM
$data = preg_replace('~&#(\d{4,5}|[2-9]\d{2,4}|1[2-9]\d);~', '&#$1;', $data);

This is used in SMF a lot. But I don't know what it is for. Can somebody enlighten me a bit? :)
Do you mean the actual regular expression or just the preg_replace function? If you mean the actual regex expression, I can't help you there, I'm no regex wizzard :(. But the preg_replace command is used to find a something with a regular expression and replace it with something else. Also look here for a guide on regex, and here for documentation of the preg_replace command.


winrules
SMF Developer
               
My Mods
Please do not PM me for support.


zosont

#2
Quote from: HarzeM on June 26, 2006, 06:22:16 PM
$data = preg_replace('~&#(\d{4,5}|[2-9]\d{2,4}|1[2-9]\d);~', '&#$1;', $data);

This is used in SMF a lot. But I don't know what it is for. Can somebody enlighten me a bit? :)
Can you give it a bit of context!
Looks like it's replacing & for just & in data formatted a number of ways.  Here we go to try and list some examples - the bit that is being checked comes straight after a #:

where after the hash there is between four and five digits.
where the digit after the hash is between 2 and 9 followed by two to four other digits.
where the digit after the hash is 1 followed by a digit between 2 and 9 followed by one more digit.

eg.
something{sometingelse
would become
something&#1 23;sometingelse (without the space between the 1 and 2 - having trouble with the post interperting that as an enconded character.)

hope that clears it up a bit

Harzem

Hi,

Thanks for the help. I know the function of regex expressions. This bit of code is used for security in SMF for various user inputs. Like when you type your signature in your Profile, the data is first processed with this regex command. I wondered the security goal of this code. What does it replace with, and why? :)

zosont

SMF seems to separate querystring arguments with ';', I suppose this regexp is just making sure HTML encoded ampersands do not interfere with that.

Advertisement: