【问题标题】:Cut string when find a space找到空格时剪切字符串
【发布时间】:2013-07-22 22:18:27
【问题描述】:

我试着剪断我的绳子,把所有东西都拿走,直到你找到“”

$ret = $html->find('td[class="date"]'); 

$pole = array();
foreach ($ret as $pole) {
    $datum[] = $pole->innertext;    // getting text what i need from HTML(simple html dom)
    }   

   echo "$datum[0]";    //output of this is: 04.07.2013 Film Europe 


$text_arr = str_split($datum[0]);      //string to array
foreach($text_arr as $x){

if($x==" ") break;   //if find space stop!
echo $x;
}

100% 我的代码是正确的,但它不起作用,echo $x 什么都不做 :),就像那个变量中没有存储任何东西一样

【问题讨论】:

  • I was 100% my code is right, but its not working mmm 是的,我以前听说过。
  • 为我工作:codepad.viper-7.com/kIE3bU 不是我会这样做

标签: php html string space


【解决方案1】:

更简单的版本

$datum[0]="04.07.2013 Film Europe"; 
$x=explode(' ',trim($datum[0]));
echo $x[0]; //04.07.2013

【讨论】:

  • 看来我在开始时也有空间,这就是为什么它不起作用,无论如何谢谢大家:)
  • 简单方法的 +1
【解决方案2】:

这样使用explode会更好:

$text_arr = explode(" ", $datum[0]);
echo $text_arr[0]; 

忘记 foreach 循环,只使用第一个元素,因为它已经在第一个空格处被剪切了。

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    实际上你应该检查字符串是否包含你想要的。 (例如开头可能有一个无意义的空格...)

    检查http://php.net/manual/de/function.preg-match.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-08
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多