Connecting Tech Pros Worldwide Help | Site Map

Searching text

  #1  
Old July 17th, 2005, 10:20 AM
SK
Guest
 
Posts: n/a
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>".



  #2  
Old July 17th, 2005, 10:21 AM
kingofkolt
Guest
 
Posts: n/a

re: Searching text


"SK" <sk@foo.com> wrote in message news:2uci4hF27hbs0U1@uni-berlin.de...[color=blue]
> 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>".
>
>[/color]

Try strip_tags():
http://us2.php.net/manual/en/function.strip-tags.php


  #3  
Old July 17th, 2005, 10:21 AM
Chung Leong
Guest
 
Posts: n/a

re: Searching text


"SK" <sk@foo.com> wrote in message news:2uci4hF27hbs0U1@uni-berlin.de...[color=blue]
> 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>".
>
>[/color]

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.


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Searching Text in PDF file Twinkle answers 1 September 14th, 2006 05:45 PM
MySQL and searching TEXT fields Michi answers 4 July 20th, 2005 01:33 AM
Searching TEXT fields Michi answers 2 July 20th, 2005 01:32 AM
Searching text files hivie answers 3 July 19th, 2005 04:02 PM