문제

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

도움이 되었습니까?

해결책

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]);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top