Forum Moderators: coopster

Message Too Old, No Replies

Inserting a new line in an HTML template

         

asantos

10:27 pm on Dec 23, 2006 (gmt 0)

10+ Year Member



Hi, right now im setting a service for blogs, which calls the whole blog's template to a var ($html) through an API (if the blog service provides one).

2nd step (where i am stuck) is to insert a javascript call in the <head> section of the template.

For example, if the $html var contains this:


<html>
<head>
<title>My Blog</title>
</head>
<body>
hi 123
</body>
</html>

After modifying the var, it should end up like this:


<html>
<head>
<title>My Blog</title>
<javascript type="text/javascript" src="http://123123.com/blog/3941"></script>
</head>
<body>
hi 123
</body>
</html>

Thats all i need to get thinks done... my idea was to search "</head>" through regular expressions and then replace that with:


<javascript type="text/javascript" src="http://123123.com/blog/3941"></script>
</head>

So, i have two questions:
1) how can i achieve that?
2) is there another (better) way?

Thanks!
andres

[edited by: asantos at 11:05 pm (utc) on Dec. 23, 2006]

asantos

10:55 pm on Dec 23, 2006 (gmt 0)

10+ Year Member



BTW, the preg should also check if the template already contains the javascript call:

<javascript type="text/javascript" src="http://123123.com/blog/?"></script>

mrnoisy

11:17 pm on Dec 23, 2006 (gmt 0)

10+ Year Member



if ($html!= '<html>
<head>
<title>My Blog</title>
<javascript type="text/javascript" src="http://123123.com/blog/'.$blogid.'"></script>
</head>
<body>
hi 123
</body>
</html>') {

$html = '<html>
<head>
<title>My Blog</title>
<javascript type="text/javascript" src="http://123123.com/blog/'.$blogid.'"></script>
</head>
<body>
hi 123
</body>
</html>';

}

asantos

11:58 pm on Dec 23, 2006 (gmt 0)

10+ Year Member



mrnoisy: that code is not optimized ;)
Actually, what i am using for now, is this:


$template = str_replace('</head>','<script src="http://example.com/b/3213"></script>'."\n".'</head>',$template);

But i still need a way to check if the javascript call is already present in the template.

[edited by: jatar_k at 5:43 am (utc) on Dec. 24, 2006]
[edit reason] examplified [/edit]

Stuperfied

3:03 am on Dec 24, 2006 (gmt 0)

10+ Year Member



if (!preg_match.........