从URL中选择hashtag到数据库

时间:2021-03-06 10:25:12

So I have a database and I store random posts such as I love #cats and I like pie. So I implemented a search function. Here's what I'm using to query

所以我有一个数据库,我存储随机帖子,比如我喜欢#cats,我喜欢馅饼。所以我实现了一个搜索功能。这是我用来查询的内容

SELECT * FROM posts WHERE post_content LIKE '%$search%' 

Now what this does is get the search result. And this is how I get $search

现在这样做是获得搜索结果。这就是我获得$ search的方式

$search = htmlspecialchars($_GET['search_query'], ENT_QUOTES, 'UTF-8');

Now say I tried searching for #cats I'd get a result. But I wouldn't get a hash tag returned.

现在说我试着搜索#cats我会得到一个结果。但我不会得到返回的哈希标签。

I'd get this

我明白了

<a href="/search/s.php?search_query=%23cats">cats</a>

Which only displays cats, and not the hashtag. This makes me believe that the hash tag isn't being retrieved by from database. Any ideas?

其中只显示猫,而不是标签。这让我相信数据库没有检索到哈希标记。有任何想法吗?

This is how i'm getting the results

这就是我得到结果的方式

$stmt = $con->prepare("SELECT * FROM posts WHERE post_content LIKE '%$search%'");
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);

2 个解决方案

#1


1  

The reason you're getting %23cats is because of URL encoding. %23 = #. It shouldn't hurt anything, since the browser should decode it properly. But if you want to display the hashtag in that hyperlink, you will have to remove the urlencode function.

你获得%23cats的原因是因为URL编码。 %23 =#。它不应该伤害任何东西,因为浏览器应该正确解码它。但是如果要在该超链接中显示主题标签,则必须删除urlencode功能。

#2


0  

In a system of hashtags is necessary that your tags assume the role of hyperlinks ...

在标签系统中,您的标签必须承担超链接的角色......

FUNCTIONS:

//Tranforme text plain in hyperlinks!
function hashtag($hashtag){
     $hastag = preg_replace('/\#([-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i', '<a id="tags" href="hashtag.php?hashtag=$1" rel="nofollow">#$1</a>', $hashtag);
     return $hashtag;
}

//Usage: echo hashtag($your_string);
//NOTE: "id" in href for customization whit css

A function to save all the tags adding commas at the end of each for when saving to the database:

保存到数据库时保存所有标记的功能,在每个标记的末尾添加逗号:

function gethashtags($text){
   //Match the hashtags
   preg_match_all('/(^|[^a-z0-9_])#([a-z0-9_]+)/i', $text, $matchedHashtags);
   $hashtag = '';
   // For each hashtag, strip all characters but alpha numeric
   if(!empty($matchedHashtags[0])){
         foreach($matchedHashtags[0] as $match){
              $hashtag .= preg_replace("/[^a-z0-9]+/i", "", $match).',';
         }
   }
   //to remove last comma in a string
   return rtrim($hashtag, ',');
}

//Usage: $your_tag_for_save = gethashtags($your_tag_content);

Save the database should be as easy as searching so I leave only the example query:

保存数据库应该像搜索一样简单,所以我只留下示例查询:

Search and Results minimal example:

<?php
   //Call to functions for this page!

   $tags = $_GET['hashtag'];
   $Search = $pdo->query("SELECT * FROM posts WHERE hashtag LIKE '%$tags%' ORDER By id DESC LIMIT 10");
   $Search->execute();

   while($fetch = $Search->fetch(PDO::FETCH_ASSOC)){

      $postID = $fetch['id'];
      $content = $fetch['content'];
?>

 <!-- Your HTML -->
 <div id="<?php echo $postID;?>"><!-- For use paggination, scroll, etc...-->

     <div id="db_post_content">
             <?php echo $content;?>
     </div>

 </div>
<?php
     }//End while !!!
?>

This example is simple returns identical tags no matter how many tags exist in the table column plus return similar results.

这个例子是简单的返回相同的标签,无论表列中存在多少个标签加上返回相似的结果。

Added a limit in the example it would be interesting to make a pagination event in case the return of very many tags.

在示例中添加了一个限制,如果返回很多标记,那么制作分页事件会很有意思。

PS: Sorry I do not speak English :P

PS:对不起,我不会说英语:P

#1


1  

The reason you're getting %23cats is because of URL encoding. %23 = #. It shouldn't hurt anything, since the browser should decode it properly. But if you want to display the hashtag in that hyperlink, you will have to remove the urlencode function.

你获得%23cats的原因是因为URL编码。 %23 =#。它不应该伤害任何东西,因为浏览器应该正确解码它。但是如果要在该超链接中显示主题标签,则必须删除urlencode功能。

#2


0  

In a system of hashtags is necessary that your tags assume the role of hyperlinks ...

在标签系统中,您的标签必须承担超链接的角色......

FUNCTIONS:

//Tranforme text plain in hyperlinks!
function hashtag($hashtag){
     $hastag = preg_replace('/\#([-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i', '<a id="tags" href="hashtag.php?hashtag=$1" rel="nofollow">#$1</a>', $hashtag);
     return $hashtag;
}

//Usage: echo hashtag($your_string);
//NOTE: "id" in href for customization whit css

A function to save all the tags adding commas at the end of each for when saving to the database:

保存到数据库时保存所有标记的功能,在每个标记的末尾添加逗号:

function gethashtags($text){
   //Match the hashtags
   preg_match_all('/(^|[^a-z0-9_])#([a-z0-9_]+)/i', $text, $matchedHashtags);
   $hashtag = '';
   // For each hashtag, strip all characters but alpha numeric
   if(!empty($matchedHashtags[0])){
         foreach($matchedHashtags[0] as $match){
              $hashtag .= preg_replace("/[^a-z0-9]+/i", "", $match).',';
         }
   }
   //to remove last comma in a string
   return rtrim($hashtag, ',');
}

//Usage: $your_tag_for_save = gethashtags($your_tag_content);

Save the database should be as easy as searching so I leave only the example query:

保存数据库应该像搜索一样简单,所以我只留下示例查询:

Search and Results minimal example:

<?php
   //Call to functions for this page!

   $tags = $_GET['hashtag'];
   $Search = $pdo->query("SELECT * FROM posts WHERE hashtag LIKE '%$tags%' ORDER By id DESC LIMIT 10");
   $Search->execute();

   while($fetch = $Search->fetch(PDO::FETCH_ASSOC)){

      $postID = $fetch['id'];
      $content = $fetch['content'];
?>

 <!-- Your HTML -->
 <div id="<?php echo $postID;?>"><!-- For use paggination, scroll, etc...-->

     <div id="db_post_content">
             <?php echo $content;?>
     </div>

 </div>
<?php
     }//End while !!!
?>

This example is simple returns identical tags no matter how many tags exist in the table column plus return similar results.

这个例子是简单的返回相同的标签,无论表列中存在多少个标签加上返回相似的结果。

Added a limit in the example it would be interesting to make a pagination event in case the return of very many tags.

在示例中添加了一个限制,如果返回很多标记,那么制作分页事件会很有意思。

PS: Sorry I do not speak English :P

PS:对不起,我不会说英语:P