close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Getting h2 and h3 headings from text in a recordset

Thread began 6/08/2021 10:19 am by Mags | Last modified 6/08/2021 1:32 pm by Ray Borduin | 177 views | 2 replies |

Mags

Getting h2 and h3 headings from text in a recordset

Hi Ray, I have some HTML text in a database and I need to extract the h2 and h3 headings from the text to construct a table of contents. I found the following bit of code whilst searching online, but having tried to replace the html text example with the contents of the recordset, I can't get it to work. This is the code:

<?php
$headings='
<h2>This is heading h2</h2>
<h3>This is heading h3</h3>
';

function fnextractHeadins($headingtag, $headings)
{
$headingText = '';
preg_match_all( '|<'.$headingtag.'>(.*)</'.$headingtag.'>|iU', $headings, $headings );
foreach($headings[0] as $headh2val)
{
$headingText.=$headh2val;
}
return $headingText;
}
echo fnextractHeadins('h2', $headings);
echo fnextractHeadins('h3', $headings);
?>

I want to replace the content shown in $headings with echo($rsRecordset->getColumnVal("ChapterText")); but my syntax must be wrong as it doesn't display anything. Can you help?

Sign in to reply to this post

Ray BorduinWebAssist

Try using:
echo($rsRecordset->getColumnVal("ChapterText",false));

The "false" disables the cross site scripting prevention code and allows html to be returned from the recordset.

Sign in to reply to this post
Did this help? Tips are appreciated...

Mags

I got it to work using the following:

<?php
$html = preg_replace_callback("#<(h[2-3])>(.*?)</\\1>#", "retitle", $rsRecordset->getColumnVal("ChapterText",false));

function retitle($match) {
list($_unused, $h2, $title) = $match;

$id = strtolower(strtr($title, " .", "--"));

echo "<$h2><a href='#$id'>$title</a></$h2>";
}
?>

This also generates an id for each h2 and h3 heading so that it can be used as an anchor link.

Might be useful to someone else!

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...