How can I search for the first 500 character, not including html tag?
Below is I came up with so far, which is searching for a keyword that occurs in a text,
SELECT *
FROM root_pages
WHERE root_pages.pg_cat_id = '2'
AND root_pages.parent_id != root_pages.pg_id
AND root_pages.pg_hide != '1'
AND root_pages.pg_url != 'cms'
AND root_pages.pg_content_1 REGEXP '[[:<:]]".$search."[[:>:]]'
OR root_pages.pg_content_2 REGEXP '[[:<:]]".$search."[[:>:]]'
ORDER BY root_pages.pg_created DESC
How can I add more conditions into it – first 500 letters that don’t include html tag?
It would be perfect if it can search the keyword on the first paragraph only – is it possible?
edit:
thanks for the help guys! this is my solution:
# query to search for “whole word match” in SQL only, e.g. when I search for "rid", it should not match "arid", but it should match "a rid".
# you can use REGEXP and the [[:<:]] and [[:>:]] word-boundary markers:
$sql = "
SELECT *
FROM root_pages
WHERE root_pages.pg_cat_id = '2'
AND root_pages.parent_id != root_pages.pg_id
AND root_pages.pg_hide != '1'
AND root_pages.pg_url != 'cms'
AND root_pages.pg_content_1 REGEXP '[[:<:]]".$search."[[:>:]]'
OR root_pages.pg_content_2 REGEXP '[[:<:]]".$search."[[:>:]]'
ORDER BY root_pages.pg_created DESC
";
# use the instantiated db connection object from the init.php, to process the query
$items = $connection -> fetch_all($sql);
$total_item = $connection -> num_rows($sql);
if ($total_item > 0)
{
foreach($items as $item)
{
# get the content
if(empty($item['pg_content_2'])) $pg_content = strip_tags($item['pg_content_1']);
else $pg_content = strip_tags($item['pg_content_2']);
# get the first 500 letters only
$pg_content = substr($pg_content, 0, 500);
# get the matches
if (preg_match("/\b(".$search.")\b/", $pg_content))
{
$match[] = $pg_content;
}
}
$total_match = count($match);
//echo $count;
}
if($total_match > 0)
{
echo '<result message="'.$total_match.' matches found! Please wait while redirecting." search="'.$search.'"/>';
}
else
{
echo '<error elementid="input" message="Sorry no results are found."/>';
}
For:
You can’t do this with MySQL only (at least for a solution that will works in 100% of the cases) – see Parsing Html The Cthulhu Way and this SO answer for more details.
PHP strip_tags and substr would help to achieve what you want.