public static function tag($rawHashtag, $postID) { preg_match_all('/#([a-zA-Z0-9 ]*)/', strtoupper($rawHashtag), $matches, PREG_PATTERN_ORDER); $arrayOfHT = $matches[1]; foreach ($arrayOfHT as $aht) { $hashtag = Hashtags::where('Hashtag', 'LIKE', trim($aht))->get()->first(); if (count($hashtag) < 1) { $hashtag = new Hashtags(); $hashtag->Hashtag = trim($aht); $hashtag->save(); } $hashtag = Hashtags::where('Hashtag', 'LIKE', trim($aht))->get()->first(); $tag = new Tags(); $tag->HashtagID = $hashtag->id; $tag->PostID = $postID; $tag->save(); } }
public function searchPostsByHashtag(Request $request) { $data = $request->all(); preg_match_all('/\\b([a-zA-Z0-9]+)\\b/', strtoupper($data['HashtagSearch']), $matches, PREG_PATTERN_ORDER); $hashtags = $matches[1]; $posts = Posts::all()->toArray(); $rank = array(); foreach ($hashtags as $ht) { foreach ($posts as $key => $post) { if (!array_key_exists($key, $rank)) { $rank += array($key => 0); } $postHashtag = Tags::where('PostID', '=', $post['id'])->get()->toArray(); foreach ($postHashtag as $pht) { if (stripos(Hashtags::find($pht['HashtagID'])->Hashtag, $ht) !== false) { $rank[$key]++; } } } } foreach ($rank as $key => $value) { if ($value < 1) { unset($rank[$key]); } } arsort($rank); $result = array(); $posts = Posts::all(); foreach ($rank as $key => $value) { $result += array($key => $posts[$key]); } preg_match_all('/\\b([a-zA-Z0-9]+)\\b/', $data['HashtagSearch'], $matches, PREG_PATTERN_ORDER); $hashtags = $matches[1]; $Hashtags = ''; foreach ($hashtags as $ht) { $Hashtags .= $ht . ' '; } return view('search')->with(['Posts' => $result, 'Hashtags' => $Hashtags]); }
/** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { if (!AuthController::checkPermission()) { return redirect('/'); } $Post = Posts::find($id); $ahtk = Tags::where('PostID', '=', $id)->get()->toArray(); $Hashtag = ''; foreach ($ahtk as $k) { $ht = Hashtags::find($k['HashtagID'])['Hashtag']; if (strlen($ht) > 0) { $Hashtag .= '#' . $ht . ' '; } } return view('admin.editpost', compact('Post') + array('Hashtag' => $Hashtag)); }