How to modify this regular expression to affect only "/..."?

I use the following regular expression to 'prepend' the base address of the website to all (href|src) on the document:

for example: http://www.mywebsite.com/basedir/

$output = preg_replace( "/(href|src){1}=(\'|\")*[\/]{1}(.*)(\'|\")*/ismU", "$1=$2{$baseaddress}$3$4", $output );

It works fine and I get results like this:

src="/windows/file.png" --->

src="http://www.mywebsite.com/basedir/windows/file.png"

But I have a problem, when src is src="//fonts.googleapis.com/fonts.css" I get: http://www.mywebsite.com/basedir//fonts.googleapis.com/fonts.css

How can I modify this regex to affect only href|src that begins with only one (1) '/ ???

Thanks.

Upvotes: 0

Views: 49

Answers (2)

Aziz Shaikh
Aziz Shaikh

Reputation: 16524

Try this:

$output = preg_replace( "/(href|src){1}=(\'|\")*[\/]{1}([^\/]*)(\'|\")*/ismU", "$1=$2{$baseaddress}$3$4", $output );

Using [^/] instead of .*

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324720

Change: [\/]{1}
To: \/(?!\/)

This will ensure that the character after that first slash is not itself a slash.

Upvotes: 1

Related Questions