I am going through a project and replacing internal links to go through a url function. I have created a bit of regex which does what I need, however I don't want it to do anything with dynamic links, as I'm going to go through and do them manually.
My regex is:
(href|src)=(?:'|")\/(?!http)(.+?)(?:'|")
My replace is:
$1="<?php echo url('$2'); ?>"
This works fine for:
<link rel='stylesheet' type='text/css' href='/public/css/print.css' media="print"/>
<link rel='stylesheet' type='text/css' href='http://example.com/public/css/print.css' media="print"/>
I want it to ignore the following though:
<link rel='stylesheet' type='text/css' href='/public/css/print<?php echo 1; ?>.css' media="print"/>
Bonus points if someone can provide a seperate regex and replace which takes the echo and places it in a string concatination of the url function! Something along the lines of
$1="<?php echo url('$2' . $3 . '$4'); ?>"
(?:'|")\/(?!http)([^<]+?)(?:'|")
will probably be good enough for the first part. Ie, replace the . with [^<] to match only strings that don't contain an opening angle bracket.
This might work ok for the second part
(?:'|")\/(?!http)([^<]+?)(<\?php[^>]*?>)?([^<]+?)(?:'|")
I would have use this one:
(href|src)=(?:'|")\/(?!http)((?!php).)+?(?:'|")
explanation:
1st Capturing group (href|src)
1st Alternative: href
href matches the characters href literally (case insensitive)
2nd Alternative: src
src matches the characters src literally (case insensitive)
= matches the character = literally
(?:'|") Non-capturing group
1st Alternative: '
' matches the character ' literally
2nd Alternative: "
" matches the characters " literally
\/ matches the character / literally
(?!http) Negative Lookahead - Assert that it is impossible to match the regex below
http matches the characters http literally (case insensitive)
2nd Capturing group ((?!php).)+?
Quantifier: +? Between one and unlimited times, as few times as possible, expanding as needed [lazy]
Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
(?!php) Negative Lookahead - Assert that it is impossible to match the regex below
php matches the characters php literally (case insensitive)
. matches any character (except newline)
(?:'|") Non-capturing group
1st Alternative: '
' matches the character ' literally
2nd Alternative: "
" matches the characters "