php - Trouble adding http://mainserver/ to all (href|action|src)=, take me into a trouble! -


i have urls...

$output = "href=\"/one/two/three\" href=\"one/two/three\" src=\"windows.jpg\" action=\"http://www.google.com/docs\""; 

when apply regular expression:

$base_url_page = "http://mainserver/"; $output = preg_replace( "/(href|src|action)(\s*)=(\s*)(\"|\')(\/+|\/*)(.*)(\"|\')/ismu", "$1=\"" . $base_url_page . "$6\"", $output ); 

i this:

$output = "href=\"http://mainserver/one/two/three\" href=\"http://mainserver/one/two/three\" src=\"http://mainserver/windows.jpg\" action=\"http://mainserver/http://www.google.com/docs\""; 

how can modify regular expression prevent this: http://mainserver/http://www.google.com/ ???????

try

$output = preg_replace( "/(href|src|action)\s*=\s*["'](?!http)\/*([^"']*)["']/ismu", "$1=\"" . $base_url_page . "$2\"", $output ); 

i have simplified regex , added lookahead makes sure string you're matching doesn't start http. now, regex allows neither single nor double quotes inside url.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -