0

これは、多くの人にとって簡単なはずです。

<?php if (stripos($_SERVER['REQUEST_URI'],'/thispage/') !== false) {echo 'This page content';} ?>

しかし、「thispage」にこの「$thisproduct['alias']」を挿入したいのですが、これを行う方法は?

私は次のように追加しようとしました:

<?php if (stripos($_SERVER['REQUEST_URI'],'/$thisproduct['alias']/') !== false) {echo 'This page content';} ?>

しかし、このエラーが発生します: 解析エラー: 構文エラー、予期しない T_STRING

4

4 に答える 4

2

あなたがするだろう:

<?php if (stripos($_SERVER['REQUEST_URI'], "/{$thisproduct['alias']}/") !== false) {echo 'This page content';} ?>

を重要視する"/{$thisproduct['alias']}/"

于 2013-02-13T17:22:29.463 に答える
1

したがって、次を使用して文字列を作成します.

stripos($_SERVER['REQUEST_URI'],'/'.$thisproduct['alias'].'/')

または二重引用符は変数を評価します

stripos($_SERVER['REQUEST_URI'],"/{$thisproduct['alias']}/")
于 2013-02-13T17:22:16.373 に答える
1

一重引用符を使用しているため、変数に実際に含まれる値ではなく、変数が文字列として扱われています。

わかる?

$array = array('foo' => 'bar);

echo $array;            //array()
echo $array['foo'];     //bar
echo '$array[\'foo\']'; //array['foo']
echo "{$array['foo']}"; //bar

/alias/を具体的に探しているのではなく、単にエイリアスを探している場合は、これで処理するのが最適です

// were $thisproduct['alias'] is now treated as a variable, not a string
if (FALSE !== stripos($_SERVER['REQUEST_URI'], $thisproduct['alias'])) 
{
    echo 'This page content';
}

さもないと

if (FALSE !== stripos($_SERVER['REQUEST_URI'], "/{$thisproduct['alias']}/")) 
{
    echo 'This page content';
}

/エイリアス/が必要な場合

于 2013-02-13T17:35:24.500 に答える
-1

以下を試してください:

"this is a text and this is a $variable " ; // using double quotes or 
"this is a test and this is a {$variable} "; // or 
"this is a test and this is a ". $variable ; 

したがって、あなたの場合、これを行うことができます:

<?php if (stripos($_SERVER['REQUEST_URI'], "/{$thisproduct['alias']}/") !== false) {echo 'This page content';} ?>

以下はhttp://php.net/manual/en/language.types.string.phpからのものです。

<?php
// Show all errors
error_reporting(E_ALL);

$great = 'fantastic';

// Won't work, outputs: This is { fantastic}
echo "This is { $great}";

// Works, outputs: This is fantastic
echo "This is {$great}";
echo "This is ${great}";

// Works
echo "This square is {$square->width}00 centimeters broad."; 


// Works, quoted keys only work using the curly brace syntax
echo "This works: {$arr['key']}";


// Works
echo "This works: {$arr[4][3]}";

// This is wrong for the same reason as $foo[bar] is wrong  outside a string.
// In other words, it will still work, but only because PHP first looks for a
// constant named foo; an error of level E_NOTICE (undefined constant) will be
// thrown.
echo "This is wrong: {$arr[foo][3]}"; 

// Works. When using multi-dimensional arrays, always use braces around arrays
// when inside of strings
echo "This works: {$arr['foo'][3]}";

// Works.
echo "This works: " . $arr['foo'][3];

echo "This works too: {$obj->values[3]->name}";

echo "This is the value of the var named $name: {${$name}}";

echo "This is the value of the var named by the return value of getName(): {${getName()}}";

echo "This is the value of the var named by the return value of \$object->getName(): {${$object->getName()}}";

// Won't work, outputs: This is the return value of getName(): {getName()}
echo "This is the return value of getName(): {getName()}";
?>
于 2013-02-13T17:23:33.047 に答える