2013-03-13 3 views
-1

Я разбираю внешний документ и делаю все ссылки в нем абсолютными. Например:Исправление относительных ссылок в PHP

<link rel="stylesheet" type="text/css" href="/css/style.css" /> 

будут заменены:

<link rel="stylesheet" type="text/css" href="http://www.hostsite.com/css/style.css" /> 

где http://www.hostsite.com является базовым URL для документа.

Это то, что я пытался и не по адресу:

$linkfix1 = str_replace('href=\"\/', 'href=\"$url\/', $code); 

Есть несколько вопросов на сайте, связанные с делать эту замену на одну строку URL, но я не мог найти, что работать на URL-адреса, встроенные в документ. Есть ли хорошие предложения о том, как сделать все эти ссылки абсолютными?

+0

HREF = "/CSS/style.css" – Drewdin

+0

Это не будет работать в этот случай, потому что я вынимаю код из другого файла. Мне нужно заменить строки в куске '$ code'. – ndm13

ответ

0

объявление общественной службы: не использовать регулярные выражения, чтобы переписать элементы форматированный документ.

Правильный способ сделать это - загрузить документ в качестве объекта (DOMDocument или SimpleXMLElement) и выполнить обработку на основе узлов и значений. Исходное решение также не обрабатывало теги src или разрешение базовых URL-адресов (например, /css/style.css).

Вот в основном правильное решение, которое может быть расширено на в случае необходимости:

# Example URL 
$url = "http://www.stackoverflow.com/"; 

# Get the root and current directory 
$pattern = "/(.*\/\/[^\/]+\/)([^?#]*\/)?/"; 
/* The pattern has two groups: one for the domain (anything before 
    the first two slashes, the slashes, anything until the next slash, 
    and the next slash) and one for the current directory (anything 
    that isn't an anchor or query string, then the last slash before 
    any anchor or query string). This yields: 
    - [0]: http://stackoverflow.com/question/123412341234 
    - [1]: http://stackoverflow.com/ 
    - [2]: question/ 
    We only need [0] (the entire match) and [1] (just the first group). 
*/ 
$matches = array(); 
preg_match($pattern, $url, $matches); 
$cd = $matches[0]; 
$root = $matches[1]; 

# Normalizes the URL on the provided element's attribute 
function normalizeAttr($element, $attr){ 
    global $pattern, $cd, $root; 
    $href = $element->getAttribute($attr); 
    # If this is an external URL, ignore 
    if(preg_match($pattern, $href)) 
     return; 
    # If this is a base-relative URL, prepend the base 
    elseif(substr($href, 0, 1) == '/') 
     $element->setAttribute($attr, $root . substr($href, 1)); 
    # If this is a relative URL, prepend the current directory 
    elseif(substr($href, 0, strlen($cd)) != $cd) 
     $element->setAttribute($attr, $cd . $href); 
} 

# Load in the data, ignoring HTML5 errors 
$page = new DOMDocument(); 
libxml_use_internal_errors(true); 
$page->loadHTMLFile($url); 
libxml_use_internal_errors(false); 
$page->normalizeDocument(); 

# Normalize <link href="..."/> 
foreach($page->getElementsByTagName('link') as $link) 
    normalizeAttr($link, 'href'); 
# Normalize <a href="...">...</a> 
foreach($page->getElementsByTagName('a') as $anchor) 
    normalizeAttr($anchor, 'href'); 
# Normalize <img src="..."/> 
foreach($page->getElementsByTagName('img') as $image) 
    normalizeAttr($image, 'src'); 
# Normalize <script src="..."></script> 
foreach($page->getElementsByTagName('script') as $script) 
    normalizeAttr($script, 'src'); 

# Render normalized data 
print $page->saveHTML(); 
1

Вам не нужно избегать двойных кавычек в строке, которая использует одинарные кавычки.

Вам также не нужно избегать косых черт.

Вы просто хотите:

str_replace('href="', 'href="http://hostsite.com', $replace_me); 

Чтобы быть в безопасности, так что вы не замените все ссылки с hostsite:

str_replace('href="/css/', 'href="http://hostsite.com/css/', $replace_me); 
+0

Спасибо большое! Знал, что это было что-то простое, что мне не хватало! Большое спасибо за ваше терпение. – ndm13

Смежные вопросы