Use a dollar sign $ cashtag as well as a hashtag # [closed]
Question
Ok I'm looking to use both dollar sign $ (cashtag) and a Hashtag # for the following code. The code below is what I've attempted to use...
This is the ORIGINAL CODE:
// Hashtags and @Mentions
$str = preg_replace_callback('~([#@])([^\s#@]+)~',
create_function('$m', '$dir = $m[1] == "#" ? "search/?q=%23" : "./";' .
'return "<a href=\"$dir$m[2]\">$m[0]</a>";' ),
$str );
/* Link text */
$str = self :: linkText( $str );
/* Emoticons */
$str = self :: emoticons( $str );
$str = stripslashes( $str );
//$str = str_replace( '<br />', '<br />', $str );
//return wordwrap( $str, 60, "\r\n", TRUE );
return $str;
}
This is my EDITED VERSION:
// Hashtags and @Mentions
$str = preg_replace_callback('~([#@$])([^\s#@$]+)~',
create_function('$m', '$dir = $m[1] == "#" ? "search/?q=%23,%24" : "./";' .
'return "<a href=\"$dir$m[2]\">$m[0]</a>";' ),
$str );
/* Link text */
$str = self :: linkText( $str );
/* Emoticons */
$str = self :: emoticons( $str );
$str = stripslashes( $str );
//$str = str_replace( '<br />', '<br />', $str );
//return wordwrap( $str, 60, "\r\n", TRUE );
return $str;
}
I added the "$" and the %24 to the above code... the thing is that the "original" code works properly and produces a URL like this: example.com/search/?q=#myhashtag
BUT with my edited code it shows a URL like this: example.com/myhashtag
How is it possible to have both "#" and "$" function like the original code and produce two URLs like this:
example.com/search/?q=#myhashtag
example.com/search/?q=$mycashtag
Solution
An unescaped $ symbol in a regular expression will anchor to the end of the input string. To match a literal $ you have to escape it with a backslash