ok so i am trying to find any href that has a css file in it and append a template directory to the beginning while keeping intact this is what i have to begin with

preg_replace('/href=(?:")[^A-Za-z0-9].css/', 'href="'.$dir.$template.'/[^A-Za-z0-9].css', $content);

i dont think that i am doing this right cause its not working.

All help would be appreciated.

thanks in advance

Recommended Answers

All 6 Replies

try:

preg_replace('/href=\s*([\x22\x27]?)([^\.]+\.css)/i', 'href=$1'.$dir.$template.'$2', $content);

first off thanks. that works like a charm.

do you happen to know of a good regular expressions tut. out of all the php coding that is the most difficult.

cool thanks

hi its me again. sry to re open this but im having trouble again. i thought that i could handle this and try and go for trying my hand at the regular expressions.

the code you gave me still works find but im noticing that on my src links that its appending a $dir.$template to links that are absolute. ex: http. now i tried to put in there grab any src that does not start with http/s like this [^http:\/\/] but i guess that is not right.

also when you do this s*() does it mean treat whats in there as a single string with 0 or more matching. and each [] within there is a variable like you put $1 and $2?

$grab = array('/<link href=(?:")http:\/\//', '/href=\s*([\x22\x27]?)([^\.]+\.css)/i', '/src=(?:")http:\/\//', '/src=(?:")/');
	$replace = array('<link href="http://', 'href=$1'.$dir.$template.'/$2', 'src="http://', 'src="'.$dir.$template.'/');

ex: http. now i tried to put in there grab any src that does not start with http/s like this [^http:\/\/] but i guess that is not right.

when using brackets, each "item" in the bracket is treated/tested independently, NOT as a UNIT. To clarify,
b[ie]d

will match "bid" OR "bed", NOT "bied". This is equivalent to:
b(i|e)d


as far as implementing [^http:\/\/] , in the tutorial, read about
Positive and Negative Lookahead
Positive and Negative Lookbehind

but for what you are trying to do, you need a slight modification to my original suggestion (provided at the end of this post)

also when you do this s*() does it mean treat whats in there as a single string with 0 or more matching.

the expression I used was \s*. From the tutorial, it should be clear that \s stands for one of the "invisible" characters (blank/spacebar, \r, \n, \f).

So that "segment" of the expression is saying "...an invisible character zero or more times." The reason for this is that you can have: href = "..." notice the spaces around the equal sign. Yet, it is still perfectly valid html. As a matter of fact, this would have been better: href\s*=\s* As far as the parenthesis go, they are capturing parenthesis. Which leads me to your next question:

...and each [] within there is a variable like you put $1 and $2?

In the expression ([\x22\x27]) , the [\x22\x27] attempts to match either an apostrophe (single quote) OR a quotation mark (double quote).
Once again, notice that is an OR match -ex: c[aou]b would match cab OR cob OR cub.

Another important thing to notice is that if you were using NEGATION, then it would be an AND match - ex:
c[^ou]b would match "c" followed by any character that
is NOT an "o"
AND
is not a "u"

followed by "b". In other words, the character between the c and the b cannot be "o" and it cannot be "u".

The parenthesis outside that expression would "capture/save" whatever is matched within the brackets (which can be either single or double quotes). Whatever it is that is matched, will be available in $1 because that is the first set of capturing parenthesis. When you have multiple sets of capturing parenthesis, $1 corresponds with the FIRST opening parenthesis, $2 corresponds with the second, etc. -ex:

//using input: "call 23" with the following regex:
//(c(a|e)ll)\s(\d+)
$1==call
$2==a
$3==23

//similarly, using input "cell 2"
$1==cell
$2==e
$3==2

but im noticing that on my src links that its appending a $dir.$template to links that are absolute

try:

preg_replace('/(href|src)\s*=\s*([\x22\x27]?)([^\x22\x27]+)\2/i', '$1=$2'.$dir.$template.'$3$2', $content);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.