【问题标题】:How to hide in text in php when serialized string is empty?序列化字符串为空时如何隐藏在php中的文本中?
【发布时间】:2023-03-20 08:46:01
【问题描述】:

我正在一个网站上工作,当serialized 字符串为空时,我想在其中隐藏文本(Hello World,A 点和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 字符串是空的)并且没有要显示的数据,那么我不想显示&lt;p&gt;Hello World 文本, Point APoint B

  • 但是如果它们中的任何一个存在(意味着serialized 字符串不为空),那么我想为Hello World&lt;p&gt; 显示&lt;p&gt; 文本,无论它存在(它可以是Point APoint B)。

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

serialized 字符串对于Point APoint B 为空时,我不希望打印这些文本。

【问题讨论】:

  • 您需要在 if 语句中添加 html
  • @Tom 你能给我指点我们怎么做吗?
  • 是的,但令人困惑的是,如果hello world 的两个部分都存在,我该如何显示它?你觉得你能举个例子吗?

标签: php string serialization


【解决方案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 放在一个共享函数中,因为您通常会做两次相同的事情。

【讨论】:

    猜你喜欢
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    • 2013-11-04
    相关资源
    最近更新 更多