So you only need part of the string? Then you need [fphp]substr[/fphp]
And you want to find the last occurrence of a # and take all text up until there --so you basically need the position of the last occurrence of # in the string–, then you need [fphp]strrpos[/fphp].
Using those two functions I’m pretty sure you can figure it out. If you have any questions let me know
Hm, I was under the impression there would be only one at the end. In that case I’d use [fphp]strpos[/fphp] rather than strrpos (strrpos would work as well, but working from left to right is a bit more natural than from right to left ;))
And you’re right, you’d work your way through the string until all the tags are gone.
A general outline:
$tweet='tweet #tweet diddly tweet #html';
while (($pos = strpos($tweet, '#')) !== false) {
// $spacepos = position of first space after $pos -- find this using strpos
// now that you know where the tag starts ($pos) and ends ($spacepos), use
// substr_replace to get it out of there
}
Twitter themselves have released code which they recommend using for parsing usernames and hash tags. I don’t know if it will do exactly as you want but it might be useful as a starting point at least. http://github.com/mzsanford/twitter-text-php
chestertondevelopment I shall take a look. Thank you.
Though I want to try the ‘manual’ way of doing things not least as it will come in handy for other parts of this project not just the hashtags. Eg. removing links. I think anyway!
Also, you should start looking for a space after the position of the #
So, $spacepos=strpos($tweet, ’ ', $pos);
Otherwise in a string ‘hello #hi bye’ it will find the space after ‘hello’, while that’s not what you want, since you want the space before ‘bye’. The code above will give you exactly that
Also, you need to take into account that there doesn’t necessarily have to a space, if #something is at the end of the string there is no space after it.
So you need something like this:
while (($pos = strpos($tweet, '#')) !== false) {
if ($spacepos = strpos($tweet, ' ', $pos)) {
$tweet=substr_replace($tweet, '', // fill this in //, // fill this in //);
} else {
$tweet=substr_replace($tweet, '', // fill this in //);
}
}
$tweet=trim($tweet);
I’ll leave the //fill this in// as an exercise to you
Yup, you could also do that, although I’d replace [^ \r\
] with [^\s] since \s=[ \r\
], and I’d remove the backreference since we’re actually interested in what it says and don’t want to capture it for later use; we just want to remove it.
Also, if you remove everything up until the next space you run into the chance that you’ll end up with two consecutive spaces in the string, so I’d add an \s to the end as well, but make it optional since it doesn’t have to be there (i.e., at the end of the string).
Lastly, if there is a tag at the end of the string we could end up with a trailing space, but that’s nothing [fphp]rtrim[/fphp] can’t handle.