tags:

views:

528

answers:

4

I need help with regular expressions. What I'm looking for is a regex that looks for link-tags like this:

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

Irrespective of where href="" is positioned, I would like to look it up in the link-tag and put a variable named $url in front of style.css with a / following. If it finds http:// or https:// in front of style.css, then i don't want to put the variable in front of it.

I want every link-tag to be replaced.

A: 

I'm guessing you're editing a single file - your text editor or IDE should be able to do a regex search/replace for you.

Try this:

Search: href="([^http].*?)"

Replace: href="<?php echo $url; ?>/\1"

If you need to use this in PHP, use preg_replace. Just remember that your search string needs a forward slash before and after it.

whichdan
This will affect hyperlinks as well, such as <a href="wherever"> so not a good idea.
The Wicked Flea
In a text editor or IDE you could replace in a selection, and in PHP you can usually parse the head separately from the body.
whichdan
+2  A: 

The solution to this will never be pretty (or reliable) using a regex, I would recommend using a DOM parser instead, and adding in the attribute with one of its manipulation methods. Have a look at simplehtmldom:

http://simplehtmldom.sourceforge.net/

For example, take a look at this:

// Create DOM from string
$html = str_get_html('<div id="hello">Hello</div><div id="world">World</div>');

$html->find('div', 1)->class = 'bar';

$html->find('div[id=hello]', 0)->innertext = 'foo';

echo $html; // Output: <div id="hello">foo</div><div id="world" class="bar">World</div>
karim79
A: 

Try this regular expression:

/(<link.*href=["'])(style.css)(["'].[^>]*>)/gi

Replace portion would look like

\1http://\2\3

or

$1http://$2$3

Note: You may need to escape one of the quotes based on how you quote the string.

null
A: 

You can use preg_replace like this to archive desired result:

preg_replace('/(<link\b.+href=")(?!http)([^"]*)(".*>)/', '$1'.$url.'$2$3$4', $html);

So this code (assuming is stored in $html and $url = 'http://mydomain.com/'):

<link rel="stylesheet" href="style.css" type="text/css">
<link rel="stylesheet" href="style2.css" type="text/css">
<link rel="stylesheet" href="http://google.com/style3.css" type="text/css">
<link rel="stylesheet" href="style4.css" type="text/css">
<link rel="stylesheet" href="https://google.com/style5.css" type="text/css">
<link rel="stylesheet" href="some/path/to/style6.css" type="text/css">

Will be converted to this:

<link rel="stylesheet" href="http://mydomain.com/style.css" type="text/css">
<link rel="stylesheet" href="http://mydomain.com/style2.css" type="text/css">
<link rel="stylesheet" href="http://google.com/style3.css" type="text/css">
<link rel="stylesheet" href="http://mydomain.com/style4.css" type="text/css">
<link rel="stylesheet" href="https://google.com/style5.css" type="text/css">
<link rel="stylesheet" href="http://mydomain.com/some/path/to/style6.css" type="text/css">
Juicy Scripter
Doing this with DOM parser is overkill (IMO) rare documents are valid (and additional processing will be needed), and DOM parsing is much more memory consuming than regexp.
Juicy Scripter