Re: [PHP] regular expression and forbidden words
Nicolas Quirin wrote: Hi, i'm french, i'm using regular expressions in php in order to rewrite hyperlink tags in a specific way before apache output is beeing sent to client. Purpose is to replace href attribute of any A html tag by a javascript function calling an ajax loader. Currently I have wrote that: $pattern = '/href="(http:\/\/dev.netdoor.fr|(\.))\/index.php\?page=(.*?)">(.*?)<\\/a>/i'; $replacement = 'OnClick="javascript:yui_fastLoader(\'./systeme/chargeur.php?page=$4\');">$5'; $html_mark = preg_replace($pattern, $replacement, $html_mark); it works, but it's too permissive, it replaces too much links (some links with some get parameters must stay unchanged) I don't really understand my pattern...it's a little strange for me...lol I wouldn't like to rewrite links that contains any of the following words in get parameters (query string): noLoader noLeftCol skipallbutpage skipservice i would like to rewrite only link that begin with 'http://dev.netdoor.fr' or '.' examples: href="http://dev.netdoor.fr/index.php?page=./comptes/index.php&noLoader&noLeftCol&idx=6&bdx=423&skipservice";>test =>must be not rewritted! href="./index.php?page=./comptes/mediatheque/index.php&filter=mov;flv;mpeg&idx=7">name =>must be rewritted like this : OnClick="javascript:yui_fastLoader(\'./systeme/chargeur.php?page=./comptes/mediatheque/index.php&filter=mov;flv;mpeg&idx=7\');">name Please help me...:0) best regards nicolas It's complicated. I'd resort to using a callback. Even if it's possible with just patterns, it wouldn't be very readable! Something like this: if (!function_exists('make_replacement')) { function make_replacement($m) { $n=$m[2]; if (preg_match('~^(http://dev.netdoor.fr|\.)/index\.php\?(.*?)$~', $n,$mm)) { if (!preg_match('~noLoader|noLeftCol|skipallbutpage|skipservice~', $mm[2])) { $n='javascript:void(0);" '. 'OnClick="javascript:yui_fastLoader(\'./systeme/chargeur.php?'. $mm[2].'\')'; } } return $m[1].'href="'.$n.'"'.$m[3]; } } $pat='~()~i'; $html_mark=preg_replace_callback($pat,'make_replacement',$html_mark); Ben. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression and forbidden words
Nicolas Quirin wrote: Hi, i'm french, i'm using regular expressions in php in order to rewrite hyperlink tags in a specific way before apache output is beeing sent to client. Purpose is to replace href attribute of any A html tag by a javascript function calling an ajax loader. Currently I have wrote that: $pattern = '/href="(http:\/\/dev.netdoor.fr|(\.))\/index.php\?page=(.*?)">(.*?)<\\/a>/i'; $replacement = 'OnClick="javascript:yui_fastLoader(\'./systeme/chargeur.php?page=$4\');">$5'; $html_mark = preg_replace($pattern, $replacement, $html_mark); it works, but it's too permissive, it replaces too much links (some links with some get parameters must stay unchanged) I don't really understand my pattern...it's a little strange for me...lol I wouldn't like to rewrite links that contains any of the following words in get parameters (query string): noLoader noLeftCol skipallbutpage skipservice i would like to rewrite only link that begin with 'http://dev.netdoor.fr' or '.' examples: href="http://dev.netdoor.fr/index.php?page=./comptes/index.php&noLoader&noLeftCol&idx=6&bdx=423&skipservice";>test =>must be not rewritted! href="./index.php?page=./comptes/mediatheque/index.php&filter=mov;flv;mpeg&idx=7">name =>must be rewritted like this : OnClick="javascript:yui_fastLoader(\'./systeme/chargeur.php?page=./comptes/mediatheque/index.php&filter=mov;flv;mpeg&idx=7\');">name Please help me...:0) best regards nicolas It's complicated. I'd resort to using a callback. Even if it's possible with just patterns, it wouldn't be very readable! Something like this: if (!function_exists('make_replacement')) { function make_replacement($m) { $n=$m[2]; if (preg_match('~^(http://dev.netdoor.fr|\.)/index\.php\?(.*?)$~',$n,$mm)) { echo "MATCHED\n"; if (!preg_match('~noLoader|noLeftCol|skipallbutpage|skipservice~',$mm[2])) { echo "NOT MATCHED\n"; $n='javascript:void(0);" '. 'OnClick="javascript:yui_fastLoader(\'./systeme/chargeur.php?'. $mm[2].'\')'; } } return $m[1].'href="'.$n.'"'.$m[3]; } } $pat='~()~i'; $html_mark=preg_replace_callback($pat,'make_replacement',$html_mark); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] regular expression and forbidden words
Hi, i'm french, i'm using regular expressions in php in order to rewrite hyperlink tags in a specific way before apache output is beeing sent to client. Purpose is to replace href attribute of any A html tag by a javascript function calling an ajax loader. Currently I have wrote that: $pattern = '/href="(http:\/\/dev.netdoor.fr|(\.))\/index.php\?page=(.*?)">(.*?)<\\/a>/i'; $replacement = 'OnClick="javascript:yui_fastLoader(\'./systeme/chargeur.php?page=$4\');">$5'; $html_mark = preg_replace($pattern, $replacement, $html_mark); it works, but it's too permissive, it replaces too much links (some links with some get parameters must stay unchanged) I don't really understand my pattern...it's a little strange for me...lol I wouldn't like to rewrite links that contains any of the following words in get parameters (query string): noLoader noLeftCol skipallbutpage skipservice i would like to rewrite only link that begin with 'http://dev.netdoor.fr' or '.' examples: href="http://dev.netdoor.fr/index.php?page=./comptes/index.php&noLoader&noLeftCol&idx=6&bdx=423&skipservice";>test =>must be not rewritted! href="./index.php?page=./comptes/mediatheque/index.php&filter=mov;flv;mpeg&idx=7">name =>must be rewritted like this : OnClick="javascript:yui_fastLoader(\'./systeme/chargeur.php?page=./comptes/mediatheque/index.php&filter=mov;flv;mpeg&idx=7\');">name Please help me...:0) best regards nicolas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] regular expression and forbidden words
Hi, i'm french, i'm using regular expressions in php in order to rewrite hyperlink tags in a specific way before apache output is beeing sent to client. Purpose is to replace href attribute of any A html tag by a javascript function calling an ajax loader. Currently I have wrote that: $pattern = '/(.*?)<\\/a>/i'; $replacement = '$5'; $html_mark = preg_replace($pattern, $replacement, $html_mark); it works, but it's too permissive, it replaces too much links (some links with some get parameters must stay unchanged) I don't really understand my pattern...it's a little strange for me...lol I wouldn't like to rewrite links that contains any of the following words in get parameters (query string): noLoader noLeftCol skipallbutpage skipservice i would like to rewrite only link that begin with 'http://dev.netdoor.fr' or '.' examples: http://dev.netdoor.fr/index.php?page=./comptes/index.php&noLoader&noLeftCol&idx=6&bdx=423&skipservice";>test =>must be not rewritted! name =>must be rewritted like this : name Please help me...:0) best regards nicolas
Re: [PHP] regular expression and forbidden words
Nicolas Quirin wrote: Hi, i'm french, i'm using regular expressions in php in order to rewrite hyperlink tags in a specific way before apache output is beeing sent to client. Purpose is to replace href attribute of any A html tag by a javascript function calling an ajax loader. Currently I have wrote that: $pattern = '/(.*?)<\\/a>/i'; $replacement = '$5'; $html_mark = preg_replace($pattern, $replacement, $html_mark); it works, but it's too permissive, it replaces too much links (some links with some get parameters must stay unchanged) I don't really understand my pattern...it's a little strange for me...lol I wouldn't like to rewrite links that contains any of the following words in get parameters (query string): noLoader noLeftCol skipallbutpage skipservice i would like to rewrite only link that begin with 'http://dev.netdoor.fr' or '.' examples: http://dev.netdoor.fr/index.php?page=./comptes/index.php&noLoader&noLeftCol&idx=6&bdx=423&skipservice";>test =>must be not rewritted! name =>must be rewritted like this : name Please help me...:0) best regards nicolas The key here is (?!) - the negative assertion, it should look something like this: $p = '#href="(?:\.|http://dev.netdoor.fr)/index\.php\?page=(?![^"]*(?:noLoader|noLeftCol|skipallbutpage|skipservice))([^"]*)">(.*?)#is'; $r = 'OnClick="javascript:yui_fastLoader(\'./systeme/chargeur.php?page=$1\');">$2'; Arpad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] regular expression and forbidden words
Hi, i'm french, i'm using regular expressions in php in order to rewrite hyperlink tags in a specific way before apache output is beeing sent to client. Purpose is to replace href attribute of any A html tag by a javascript function calling an ajax loader. Currently I have wrote that: $pattern = '/(.*?)<\\/a>/i'; $replacement = '$5'; $html_mark = preg_replace($pattern, $replacement, $html_mark); it works, but it's too permissive, it replaces too much links (some links with some get parameters must stay unchanged) I don't really understand my pattern...it's a little strange for me...lol I wouldn't like to rewrite links that contains any of the following words in get parameters (query string): noLoader noLeftCol skipallbutpage skipservice i would like to rewrite only link that begin with 'http://dev.netdoor.fr' or '.' examples: http://dev.netdoor.fr/index.php?page=./comptes/index.php&noLoader&noLeftCol&idx=6&bdx=423&skipservice";>test =>must be not rewritted! name =>must be rewritted like this : name Please help me...:0) best regards nicolas