Jump to content

Quick regex question


formasfunction

Recommended Posts

I'm trying to write a regular expression that will take a chunk of html and modify the body tag but I'm having trouble as this is my first regex attempt.  Here's what I have:

 

$_response_body = whatever html I'm feeding in
$pattern = '<body \b[^>]*>';
$replacement = '\\1 <div id="top_frame"><div id="nav_bar">text</div><div id="sub_bar">text</div></div>';
echo $_response_body = preg_replace($pattern, $replacement, $_response_body);

 

So I'm hoping that this would take something like <body class="home"> or <body class="main"> and turn them into:

 

<body class="home"><div id="top_frame"><div id="nav_bar">text</div><div id="sub_bar">text</div></div>

or

<body class="main"><div id="top_frame"><div id="nav_bar">text</div><div id="sub_bar">text</div></div>

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/52012-quick-regex-question/
Share on other sites

Sorry, I don't think I was clear enough.  I don't want to change the class of the body, I want to find the opening body tag regardless of whether or not it has a class attached to it and add a chunk of code after it.  If it was always a predictable tag (like always just <body> with no class) then I'd use a str_replace and append my new code at the end.

Link to comment
https://forums.phpfreaks.com/topic/52012-quick-regex-question/#findComment-256388
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.