当序列化字符串为空时,如何在php中隐藏文本?

时间:2018-08-16 18:08:02

标签: php string serialization

我正在一个网站上,当serialized字符串为空时,我想在其中隐藏文本(Hello World,Point A和Point B)。下面是我的代码:

<p class="font-weight-bold">Hello World</p>
<p class="font-weight-bold">Point A</p>

<?php

   $serialized = '';
   for ($i = 0; $i < count($data['item']->process_out); $i++) {
       if(strcmp($data['item']->process_out[$i]->processs_type, "pickup") == 0)
       {
          $serialized .= strtolower($data['item']->process_out[$i]->processs_times);
       }
   }

   if($serialized != '')
   {
      $unserialized = unserialize( $serialized );    
      foreach($unserialized  as $key=>$value)
      {
         echo $key." ".$value['start']." ".$value['end']."<br/>";
      }
   }
?>

<p class="mt-2 font-weight-bold text-center">Point B</p>
<?php

   $serialized = '';

   for ($i = 0; $i < count($data['item']->process_out); $i++) {
       if(strcmp($data['item']->process_out[$i]->processs_type, "location_pickup") == 0)
       {
          $serialized .= strtolower($data['item']->process_out[$i]->processs_times);
       }
   }

   if($serialized != '')
   {
      $unserialized = unserialize( $serialized );  
      foreach($unserialized  as $key=>$value)
      {
         echo $key." ".$value['start']." ".$value['end']."<br/>";
      }
   }
?>

问题陈述:

Hello World内,我有两个部分-Point APoint B

  • 如果Point APoint B均为空(表示serialized字符串为空)并且没有可显示的数据,那么我不想显示{{1 }} <p>Hello WorldPoint A的全部文字。

  • 但是,如果其中任何一个存在(表示Point B字符串不为空),那么我想显示serialized的{​​{1}}文本和<p>的文本不论存在什么(可以是Hello World<p>)。

这有可能吗?当序列化字符串为空时,其显示如下:

enter image description here

Point APoint B的{​​{1}}字符串为空时,我不希望打印这些文本。

1 个答案:

答案 0 :(得分:1)

您应该将html输出放入几个变量中(包括“ Hello World”),并在完成所有测试后最后进行输出。

<?php
// pre-set/initialize some html templates/strings to be used later
$html = '';
$helloWorld_header = '<p class="font-weight-bold">Hello World</p>';
$pointA_header = '<p class="font-weight-bold">Point A</p>';
$pointA_content = '';
$pointB_header = '<p class="mt-2 font-weight-bold text-center">Point B</p>';
$pointB_content = '';
$pointA_found = $pointB_found = false;  // initialize both to false



   $pointA_serialized = '';
   for ($i = 0; $i < count($data['item']->process_out); $i++) {
       if(strcmp($data['item']->process_out[$i]->processs_type, "pickup") == 0) {
          $pointA_serialized .= strtolower($data['item']->process_out[$i]->processs_times);
       }
   }

   if($pointA_serialized != '') {
      $pointA_found = true;
      $unserialized = unserialize( $pointA_serialized );
      foreach($unserialized  as $key=>$value) {
         $pointA_content .= $key." ".$value['start']." ".$value['end']."<br/>";
      }
      // add everything from pointA to $html
      $html .= $pointA_header . $pointA_content;
   }

   $pointB_serialized = '';

   for ($i = 0; $i < count($data['item']->process_out); $i++) {
       if(strcmp($data['item']->process_out[$i]->processs_type, "location_pickup") == 0) {
          $pointB_serialized .= strtolower($data['item']->process_out[$i]->processs_times);
       }
   }

   if($pointB_serialized != '') {
      $pointB_found = true;
      $unserialized = unserialize( $pointB_serialized );  
      foreach($unserialized  as $key=>$value) {
         $pointB_content . $key." ".$value['start']." ".$value['end']."<br/>";
      }
      // add everything from pointB to $html
      $html .= $pointB_header . $pointB_content;
   }

// if pointA or pointB is set/found...
if($pointA_found || $pointB_found) {
   // ... add the main header before the rest
   $html = $helloWorld_header + $html;
}

// do the actual output
echo $html
?>

这只是您所拥有内容的快速修复。会有更优雅的方法做到这一点。我会将pointA和pointB放在一个共享函数中,因为您通常会做两次相同的事情。