467,903 Members | 1,863 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,903 developers. It's quick & easy.

Searching text

SK
Is there a way to store HTML into a MySQL TEXT column,
yet be able to search over textual content only?

For example, if I store the following HTML snippet:

<p>A very <em>small</em> paragraph.</p>

.... into an TEXT column, I would like to be able to search
in the text "A very small paragraph." only, meaning the search
would find "small" (even if it's enclosed in <em> tags and
thus creating string "<em>small</em>"), and ignore search
strings "<p>" and "<em>".

Jul 17 '05 #1
  • viewed: 1633
Share:
2 Replies
"SK" <sk@foo.com> wrote in message news:2u*************@uni-berlin.de...
Is there a way to store HTML into a MySQL TEXT column,
yet be able to search over textual content only?

For example, if I store the following HTML snippet:

<p>A very <em>small</em> paragraph.</p>

... into an TEXT column, I would like to be able to search
in the text "A very small paragraph." only, meaning the search
would find "small" (even if it's enclosed in <em> tags and
thus creating string "<em>small</em>"), and ignore search
strings "<p>" and "<em>".


Try strip_tags():
http://us2.php.net/manual/en/function.strip-tags.php
Jul 17 '05 #2
"SK" <sk@foo.com> wrote in message news:2u*************@uni-berlin.de...
Is there a way to store HTML into a MySQL TEXT column,
yet be able to search over textual content only?

For example, if I store the following HTML snippet:

<p>A very <em>small</em> paragraph.</p>

... into an TEXT column, I would like to be able to search
in the text "A very small paragraph." only, meaning the search
would find "small" (even if it's enclosed in <em> tags and
thus creating string "<em>small</em>"), and ignore search
strings "<p>" and "<em>".


One possible way to do this would be to strip out the tags before you save
the text and store the formatting info separately. Restore the tags only
when you need to output in HTML.

Example:

<?

$example = <<<EXAMPLE
<p>A very <em>small</em> paragraph.</p>

.... into an TEXT column, I would like to be able to search
in the text "A very small paragraph." only, meaning the search
would find "small" (even if it's enclosed in <em> tags and
thus creating string "<em>small</em>"), and ignore search
strings "<p>" and "<em>".
EXAMPLE;

function ExtractFormatting($html) {
preg_match_all('/<.*?>/', $html, $matches,
PREG_OFFSET_CAPTURE |PREG_PATTERN_ORDER);
$tags = $matches[0];
$formatting = array();
foreach($tags as $tag) {
list($tag_html, $tag_offset) = $tag;
$formatting[$tag_offset] = $tag_html;
}
$text = preg_replace('/<.*?>/', '', $html);
return array($text, $formatting);
}

function ApplyFormatting($text, $formatting) {
$html = $text;
foreach($formatting as $tag_offset => $tag_html) {
$html = substr($html, 0, $tag_offset)
. $tag_html
. substr($html, $tag_offset);
}
return $html;
}

list($text, $formatting) = ExtractFormatting($example);

echo "<pre style='border:1px solid black'>$text</pre>";

$html = ApplyFormatting($text, $formatting);

echo "<div style='border:1px solid black'>$html</div>";

?>

To store the formatting info, just serialize the array and dump it into
another text column.
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Michi | last post: by
4 posts views Thread by Michi | last post: by
2 posts views Thread by Roberto Dias | last post: by
8 posts views Thread by Gordon Knote | last post: by
3 posts views Thread by Aaron | last post: by
4 posts views Thread by Costa | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.