【问题标题】:How find words and abolish (delete) characters after it based in a list?如何在列表中查找单词并取消(删除)字符?
【发布时间】:2015-05-24 15:26:24
【问题描述】:

如果我想检查字符串中是否存在“php”并删除它后面的单词,我可以使用:

$string = 'hello world php is a bla bla..';
$string = explode(' php ', $string);
echo $string[0];

但是如果我有多个单词而不仅仅是 'php' 怎么办?例如下面的几首歌曲名称有'feats','feat','ft','ft.'..这是其中的四个。

我想让“sia - elastic feat brunos mars”变成“sia - elastic”。

【问题讨论】:

  • 试试str_replace('php', '' ,$string)
  • @NarendraSisodia 通过使用 explode 我可以做同样的事情,但是多个单词呢?我不想写重复的代码。

标签: php arrays string substring


【解决方案1】:

这是您需要的代码:

<?php

$string = 'sia - elastic feat brunos mars';

$array = array('feats', 'feat', 'ft', 'ft.');

for($i = 0; $i < count($array); $i++) {
  $out = explode(' ' . $array[$i] . ' ', $string);
  if (count($out) > 1) {
       break;
  }   
}

echo $out[0];

?>

【讨论】:

    【解决方案2】:

    你可以使用

     $string = 'hello world php is a bla bla..';
     $string_before = strstr($string, 'php', true); 
    

    这将检查字符串“php”的第一次出现。然后删除其余的。

    【讨论】:

      猜你喜欢
      • 2021-02-15
      • 1970-01-01
      • 2017-01-18
      • 1970-01-01
      • 2011-03-31
      • 1970-01-01
      • 2011-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多