php如何从变量中获取特定数据

php如何从变量中获取特定数据,php,arrays,Php,Arrays,我有一个包含以下数据的变量 $cheers="<p>content:op=This is an apple</p> <p>content:op=This is a school bus</p> <p>content:op=This is PHP code</p> " ; 谢谢假设您正在网页上显示此内容,并且浏览器已准备就绪,您只需回显变量的值即可。不确定是否需要常数:op部分,这似乎不是必需的。它可以像下面这样重写

我有一个包含以下数据的变量

$cheers="<p>content:op=This is an apple</p> <p>content:op=This is a school bus</p> <p>content:op=This is PHP code</p> " ;

谢谢

假设您正在网页上显示此内容,并且浏览器已准备就绪,您只需回显变量的值即可。不确定是否需要常数:op部分,这似乎不是必需的。它可以像下面这样重写

    <?php
    // define variable
    $cheers="<p>content:op=This is an apple</p> <p>content:op=This is a school bus</p> <p>content:op=This is PHP code</p> " ;

    // remove extra text
    $new_cheers = str_replace("content:op=","",$cheers); 

    // split paragraphs (if needed, otherwise just echo $new_cheers)
    $paragraphs = explode(" ", $new_cheers); 
    echo $paragraphs[0]; 
    echo $paragraphs[1]; 
    echo $paragraphs[2];
    ?>

可选(跳过上面的$段落部分,只在下面输出新的欢呼声):


测验
你的产出

由于您已经有了HTML标记,浏览器将读取格式并正确地呈现它。

如果您正在分析字符串,这应该适合您

<?php
   $cheers="<p>content:op=This is an apple</p> <p>content:op=This is a school bus</p> <p>content:op=This is PHP code</p>";
   $cheers = strip_tags($cheers);
   $arrtext = explode("content:op=", $cheers);

   foreach ($arrtext as $element){
      if ($element!=""){
            echo $element."\n\r";
       }
    }

preg\u match\u all
this=>
/这看起来像是可以用正则表达式轻松解析的东西。你说“输出变量”-在哪里?在浏览器中?如果是这样,只需
echo$yourvariable可以工作。但是如何获取内容的raid:op=“在开始时”和“

在结束时…伙计们,我做了一些更改,现在看看。谢谢若您需要单词的颜色,我将构建一个术语数组,然后您控制输出的格式。例如$items=Array(“苹果”、“校车”、“php代码”);然后遍历下面的项目,并在显示中添加HTML格式,而不是数据。”foreach$项目在$项目中 这是

。。。end foreach.Mike my变量包含以下数据….$cheers=“content:op=这是一个苹果content:op=这是一辆校车content:op=这是PHP代码”;您可以使用子字符串,也可以使用str\u replace。。。见:1。拆分字符串并可能使用explode$items=explode(“,$cheers);//这将为您提供项的列表。
<html>
<head><title>Test</title></head>
<body>
<h1>Your output</h1>
<?php echo $new_cheers; ?>
</body>
</html>
<?php
   $cheers="<p>content:op=This is an apple</p> <p>content:op=This is a school bus</p> <p>content:op=This is PHP code</p>";
   $cheers = strip_tags($cheers);
   $arrtext = explode("content:op=", $cheers);

   foreach ($arrtext as $element){
      if ($element!=""){
            echo $element."\n\r";
       }
    }