Question

i want to ask about twitter search engine, i want it for my web, for example

$data; //it's from search engine form 

i want if $data beginning with # then i use mysql query for searching data that contain hashtag

and if $data beginning with @ then use mysql query for searching data that contain @

i want $data has been filtered before query

mysqli_query($con,"SELECT*FROM table WHERE data LIKE '%$data%'");

how to check data if it has hashtag or @? thank you

Was it helpful?

Solution

switch (substr($data,0,1)){
    case '@': //if $data begins with @
        mysqli_query($con,"SELECT * FROM table WHERE data LIKE '%$data%'");
    break;
    case '#': //if $data begins with #
        mysqli_query($con,"SELECT * FROM table WHERE data LIKE '%$data%'");
    break;
    default: //neither of above
        echo "your query doesn't begin with @ or #";   
}

Edit for comment below :

You are looking for preg_match_all() here is an example for what you want to do :

This will create 2 arrays ($hashes, and $arobases) containing the strings you want.

preg_match_all("(#[^\s]+)", "hello : #hash1 #hash2 @at1 @at2", $hashes);
preg_match_all("(@[^\s]+)", "hello : #hash1 #hash2 @at1 @at2", $arobases);
var_dump($hashes[0]);
echo "<br/><hr/>";
var_dump($arobases[0]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top