Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.