【问题标题】:How can I find a special character in my string and store all value after this character in a variable [duplicate]如何在我的字符串中找到一个特殊字符并将该字符之后的所有值存储在变量中[重复]
【发布时间】:2014-04-09 10:58:08
【问题描述】:

我正在尝试从我的字符串中查找特殊字符 $@ 并尝试将这些特殊字符之后的所有值存储在变量中,但我无法执行此任务。如何在我的整个字符串中找到这个 $@ 特殊字符并将这个特殊字符之后的所有值存储在一个变量中。我的字符串是:

 92~SAPL/1200/2012~2~LAXMAN SINGH AND OTHERS~SHANKER SINGH AND OTHERS~D~
 2014-04-10~09:31:13
 07/04/2014|93~SAPL/275/1998~1~RAM SWAROOP AND OTHERS~BABU RAM AND OTHERS~D~
 2014-04-10~09:31:13 07/04/2014|94~FAFOD/52/2013~3~RAM KUMAR PANDEY~RAJENDRA PANDEY AND
 ANOTHER~D~2014-04-10~09:31:13 07/04/2014$@2014-04-10~2014-04-09 

【问题讨论】:

  • echo substr($string, strpos($string, '$@') + 1);

标签: php


【解决方案1】:

试试这个:

$string = "Your Entire String";
$explodedString = explode('$@', $string);

if(count($explodedString) > 1) {
   unset($explodedString[0]);
   $returnString = count($explodedString) > 1 ? $explodedString : $explodedString[0];
} else {
   $returnString = null;
}

if(is_array($returnString)) {
   //if you want to divide the string only to the first occurrence of $@ leave the below line as is else comment it out
   $returnString = implode('', $returnString);
}

echo $returnString;

【讨论】:

  • 当一个简单的substr 可以做到这一点时,为什么要这样做?
  • @JakeGould,一个 substr 肯定会做到这一点,但是,如果用户想要让它爆炸并将它放在一个数组中,为什么不使用这个小函数来做到这一点。问这个问题的人是论坛的新手。这样的脚本可能会帮助他克服代码中任何地方的问题。希望你理解
猜你喜欢
  • 2015-10-17
  • 2018-12-01
  • 2017-03-09
  • 2020-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多