Wednesday 15 April 2015

php - RegEx: Match URL including tilde -



php - RegEx: Match URL including tilde -

i need match relative urls in code, , replace absolute urls. in these relative urls there ~ character, can't seem match.

$page = preg_replace('/src="(\/)?([\w_\-\/\.\?&=@%#]*)"/i','src="' . $url . '$2"', $page); $page = preg_replace('/href="(\/)?([\w_\-\/\.\?&=@%#]*)"/i','href="' . $url . '$2"', $page);

this should replace urls, doesn't work these urls:

https://www.domain.dk/~/media/2561bd6afbd64402877e4aced01f97fd.ashx /cassette.axd/stylesheet/fdbdaa59cb97b35f06f65fd41cb60caa3975cc0f/forbrug-rwd_(max-width 767px)

how that?

i utilize regex:

(?:src="|href=")(?!http)\/?(.+?)"

debuggex demo

this allow grab urls in src or href attributes, doesn't start http (and it'll care of presence or not of leading slash):

$page = preg_replace('/(?:src="|href=")(?!http)\/?(.+)"/i', $url . '$1', $page);

php regex

No comments:

Post a Comment