我需要用php显示特定的字符串, 例如:
<?php
$text = "the grass is green"
echo $text;
?>
输出结果为the grass is green
,但如何只展示grass
代替the grass is green
?
答案 0 :(得分:1)
<?php
$text = "grass"
echo $text;
?>
答案 1 :(得分:0)
答案 2 :(得分:0)
如果您事先知道指定的单词,那么这样的话就可以了:
$word = 'green'; // you know your keyword
$text = "the grass is green" // your phrase
$a = explode(" ", $text); // create array of all words in $text
foreach ($a as $b) // loop through array
{
if ($b==$word) // if match is found, echo it
{
echo $b;
break;
}
}
如果另一方面,你的话总是在指定的位置,那么这样的事情会对你有帮助:
$wordPosition = 2; // you know your keyword place in phrase
$text = "the grass is green" // your phrase
$a = explode(" ", $text); // create array of all words in $text
echo $a[$wordPosition]; // echo the specified word, by position
答案 3 :(得分:0)
您可以使用这种方式获取指定的字符串。
$ text =&#34;草是绿色的&#34 ;;
$ textArray = explode(&#39;&#39;,$ text);
echo $ name = $ textArray [1];
输出:草
...享受