如何在php中显示特定的单词?

时间:2014-06-26 07:48:20

标签: php html

我需要用php显示特定的字符串, 例如:

<?php
$text = "the grass is green"
echo $text;
?>

输出结果为the grass is green,但如何只展示grass代替the grass is green

4 个答案:

答案 0 :(得分:1)

<?php
$text = "grass"
echo $text;
?>

答案 1 :(得分:0)

使用字符串函数http://www.php.net/manual/en/ref.strings.php

在你的情况下

echo substr($text,14);

答案 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];

输出:草

...享受