if ($_POST['id'] beginsWith "gm") {
$_SESSION['game']=="gmod"
}
if ($_POST['id'] beginsWith "tf2") {
$_SESSION['game']=="tf2"
}
¿Cómo se hace esto para que funcione?si la cadena comienza con "xx" (PHP)
if ($_POST['id'] beginsWith "gm") {
$_SESSION['game']=="gmod"
}
if ($_POST['id'] beginsWith "tf2") {
$_SESSION['game']=="tf2"
}
¿Cómo se hace esto para que funcione?si la cadena comienza con "xx" (PHP)
if (strpos($_POST['id'], "gm") === 0) {
$_SESSION['game'] ="gmod"
}
if (strpos($_POST['id'],"tf2") === 0) {
$_SESSION['game'] ="tf2"
}
Esto está mal. Si strpos devuelve false, ambas declaraciones if se evaluarán como verdaderas. – andrewtweber
@andrewtweber: Ahí. Reemplazado el '==' con '===' :-P –
Usted podría utilizar substring
if(substr($POST['id'],0,3) == 'tf2')
{
//Do something
}
Editar: fija una función incorrecta (substring()
utilizado, debe ser substr()
)
Puede escribir un begins_with
usando strpos
:
function begins_with($haystack, $needle) {
return strpos($haystack, $needle) === 0;
}
if (begins_with($_POST['id'], "gm")) {
$_SESSION['game']=="gmod"
}
// etc
function startswith($haystack, $needle){
return strpos($haystack, $needle) === 0;
}
if (startswith($_POST['id'], 'gm')) {
$_SESSION['game'] = 'gmod';
}
if (startswith($_POST['id'], 'tf2')) {
$_SESSION['game'] = 'tf2';
}
No te que al asignar valores a uso variable de una sola =
la manera más rápida de hacerlo, pero se pueden utilizar expresiones regulares
if (preg_match("/^gm/", $_POST['id'])) {
$_SESSION['game']=="gmod"
}
if (preg_match("/^tf2/, $_POST['id'])) {
$_SESSION['game']=="tf2"
}
usted tiene que escribir un parche en el código C que añade esta palabra clave/sintaxis – Gordon