php - preg_replace - adding a protocol to href and src elements -


is possible add protocol urls (href & src) don't contain protocols ?

for example, replace url:

<a href="/page/image.png" target="_blank">text</a> 

to:

<a href="http://my-webpage.com/page/image.png" target="_blank">text</a> 

but important 2 things:

  • if original url in href/src starting slash "/", protocol domain should add without slash on end when original url isn't starting slash - protocol domain should add slash,

  • if original url starting "../" or "./" etc. - should remove , then, protocol domain should add slash.

is possible in 1 regex ?

thanks.

edit:

there code:

$url = 'http://my-page.com/'; $html = file_get_contents($url); preg_match('"charset=([a-za-z0-9\-]+)"si', $html, $charset); $charset = strlen($charset[1]) > 3 ? $charset[1] : 'utf-8'; $html = mb_convert_encoding($html, 'html-entities', $charset);  preg_match_all('"href=\"(.*?)\""si', $html, $matches);  foreach($matches[1] $key => $value) {     if ( preg_match("/^(http|https):/", $value) )     {         continue;     }      $html = str_replace($value, $url.$value, $html); }  preg_match_all('"src=\"(.*?)\""si', $html, $matches);  foreach($matches[1] $key => $value) {     if ( preg_match("/^(http|https):/", $value) )     {         continue;     }      $html = str_replace($value, $url.$value, $html); }  echo $html; 

i use regex in sed or other recipe:

sed 's/href="/href="http://site.domain/g' 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -