regex - How to replace http for https in string but only on specific domains in PHP -
i need parse strings of html content , possible replace urls images on other domains https wherever http. issue not external domains support https can't blanket replace http https.
so want list of domains know work https.
there small added complication search has work domains irrelevant if www. added or not.
using example given @wiktor have close want, needs reversing run replace when match found, not when match isn't found code functions.
/http(?!:\/\/(?:[^\/]+\.)?(?:example\.com|main\.com)\b)/i
i believe can use
$domains = array("example.com", "main.com"); $s = "http://example.com http://main.main.com http://let.com"; $re = '/http(?=:\/\/(?:[^\/]+\.)?(?:' . implode("|", array_map(function ($x) { return preg_quote($x); }, $domains)) . ')\b)/i'; echo preg_replace($re, "https", $s); // => https://example.com https://main.main.com http://let.com see ideone demo
the regex matches:
http-httpif followed by...(?=- start of positive lookahead:\/\/-://literal substring(?:[^\/]+\.)?- optional sequence of 1+ chars other/,.(?:+implodecode - creates alternation group escaping individual literal branches (to match 1 of alternatives,exampleormain, etc.))- end of alternation group
\b- word boundary)- end of lookahead/i- case insenstive modifier.
Comments
Post a Comment