Relatively new to php and looking for some help in updating links on a specific page. The page has numerous links eg. href=/link/
and I would like to code the page to identify these links (links that do not already have http
or https
) and prepend with a url eg. www.domain.com
to each. Basically ending up with href=www.domain.com/link/
. Any help would be greatly appreciated.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
Maybe it suffices to just change the base URI of the document with the
BASE
element:With this the new base URI is
http://example.com/link/
instead of the URI of the document. That means, every relative URI is resolved fromhttp://example.com/link/
instead of the document’s URI.You could always use output buffering at the top of your page with a callback that reformats your hrefs to how you'd like them:
Because you left out critical details in your first question, here is the second answer.
Doing what
@Nev Stokes
says may work, but it will also get more than tags. You should never use regular expressions (or, worse, strp_replace) on HTML.Instead, use the
file_get_html()
library and do this:Output:
I think you want to parse a list of URLs and prepend "http://" to the ones that don't have it.