Usa un simbolo del dollaro cash cashtag e un hashtag # [chiuso]
Domanda
Ok sto cercando di usare sia il segno del dollaro $ (cashtag) che un Hashtag # per il seguente codice.Il codice qui sotto è quello che ho tentato di usare...
Questo è il CODICE ORIGINALE:
// 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;
}
Questa è la mia VERSIONE MODIFICATA:
// 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;
}
Ho aggiunto " $ " e %24 al codice sopra...il fatto è che il "originale"il codice funziona correttamente e produce un URL come questo:example.com/search/?q=#myhashtag
MA con il mio codice modificato mostra un URL come questo:example.com/myhashtag
Come è possibile avere sia la funzione " # " che " $ " come il codice originale e produrre due URL come questo:
example.com/search/?q=#myhashtag
example.com/search/?q=myc mycashtag
Soluzione
Un simbolo un senza escape in un'espressione regolare si ancorerà alla fine della stringa di input.Per abbinare un letterale have devi sfuggirlo con una barra rovesciata