Search code examples
phptagging

PHP tagging system


I have this script:

<?php

    $string = "@Erol Simsir @Hakan Simsir";
    $output = preg_replace("/@([^\s]+)/", "<a href=\"?tag=$1\">$1</a>", $string);
    echo $output;

?>

This script detects all words with a '@' symbol in front of them and changes them into links/tags. However, I don't want a hashtag system but a tagging system, like on Twitter, where you can do '@JohnDoe' and the user JohnDoe will be the person the Tweet goes to.

So, what I need is to store all the tags in a string in an array to use them for a SQL query afterwards.

How can I achieve this?

UPDATE

I have this code right now:

$string  = $_POST["tags"];
$output = preg_replace("/@([^\s]+)/", "<a href=\"?tag=$1\">$1</a>", $string);
$finalOutput = explode(" ", $string);
$count = count($finalOutput);
$i = 0;
while($i < $count) {
echo $finalOutput[$i];
$i = $i + 1;
}

Problem is, that the tags look like this in the output: @john @sandra etc. How can I remove the @ symbol in the output?


Solution

  • $array = explode(' ', $string);
    

    give a try to this code :)

    $count = count($array);
    $tag_array = array();
    $j=0;
    for($i=0;$i<$count;$i++)
    {
      if(substr($array[$i],0,1)==='@')
      {
        $tag= ltrim ($array[$i],'@');
        $tag_array[$j] = $tag;
        $j++;
      } 
    }
    
    print_r($tag_array);
    

    let me know if you want any further help :)