1

I am noob at php, but I need to alter a script a tiny bit.

It looks through a HTML template and looks for the <head> , and then just after it - it adds a pre-defined content (css and javascript paths etc) for the <head> area in the HTML output.

Now the function looks like:

$template = preg_replace('/<head(.*?)\>(.*?)/is', "<head\\1>".'[|EXTRAHEADER|]'

I see that this takes ALL areas that start with that is inside a new theme I am working with.

How can I change the above identifier/ what it looks for - so it does NOT choose

<header...

but only

<head...

?

I imagine the original owner of the script has done the above to accept people to use both <head> and <head >, but I really don't know for sure.

For me it is just important that this script does not try to add all css etc "head content", to other places in the HTML document than in the HTML-head.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
Preben
  • 1,277
  • 2
  • 11
  • 20

3 Answers3

0

Try and change:

$template = preg_replace('/<head(.*?)\>(.*?)/is', "<head\\1>".'[|EXTRAHEADER|]'

to this:

$template = preg_replace('/<head\b(.*?)\>(.*?)/is', "<head\\1>".'[|EXTRAHEADER|]'
Cristofor
  • 2,077
  • 2
  • 15
  • 23
0

Please try this

$template = preg_replace('/<head\b(.*?)\>(.*?)/is', "<head\\1>".'[|EXTRAHEADER|]'
Marecky
  • 1,924
  • 2
  • 25
  • 39
  • This worked :-) - What does the "b" mean? – Preben Jul 09 '14 at 13:20
  • It means word boundaries, it is well described here. http://www.regular-expressions.info/wordboundaries.html _There are three different positions that qualify as word boundaries: Before the first character in the string, if the first character is a word character. After the last character in the string, if the last character is a word character. Between two characters in the string, where one is a word character and the other is not a word character._ – Marecky Jul 09 '14 at 13:39
  • Please upvote my answer, just press up arrow above zero number to the left of `$template` :-) – Marecky Jul 09 '14 at 14:18
  • 1
    I have tried, but get a message that I am not allowed as I have to few "reputations" – Preben Jul 09 '14 at 14:36
0

Please try

$template = preg_replace('/<head\>(.*?)/is', '<head>[|EXTRAHEADER|]'.$1);

Without knowing your exact purpose, I'm just guessing here.

Is that extraheader gonna be parsed by a template engine afterwards?

ffflabs
  • 17,166
  • 5
  • 51
  • 77
  • Yes, it is a part of a long list of parsers. To me it looks like it parses the main parts of the HTML document first, and then put it all together in a last parser that combines everything. – Preben Jul 09 '14 at 13:26