0

Hey guys so I have a question,

Lets say someone puts "Hi lets have #fun and than more #fun" Now lets say I want to grab the second hashtag including the word connected to it and store it into a variable.

How would I go about getting the second part because using (strpos($a,'#fun') would only return the first one.

Thank you for your time!

David

Edit:

Here's what I did:

$code = "Hi lets have #funfgs and than more #funny";
$pos1 = strpos($code, '#');
$pos2 = strpos($code, '#', $pos1 + strlen('#'));
echo substr($code, $pos2);
David Biga
  • 2,763
  • 8
  • 38
  • 61
  • @DipeshParmar will this work even if there are other words in between the first and second parameter I want, they will always be different. The main thing I point out is the # – David Biga Mar 22 '13 at 04:35
  • @DavidBiga yes, it should still work. It's just using `strpos` on a haystack that has the first occurrence of the needle stripped out. – Nick Pickering Mar 22 '13 at 04:48

1 Answers1

1

You can use following Regular Expression

$code = "Hi lets have #fun and than more #fun";

$pattern = "$#[^\s]*$i";

preg_match_all($pattern, $code, $matches);

print_r($matches);
user1190992
  • 669
  • 3
  • 8