【问题标题】:delete specific characters from start to end of string PHP从字符串PHP的开头到结尾删除特定字符
【发布时间】:2017-09-24 08:39:18
【问题描述】:

我有这样的刺:

$content = "[video width="640" height="360" mp4="http://click.ir/wp-content/uploads/2017/09/Spinning_cylinder_shaped_elevator.mp4"][/video]"

我想从'[video' to 'mp4="'中删除... PHP中有built-infunction或者解决这个问题的解决方案吗?

宽度和高度不是静态的或在我的问题中定义

【问题讨论】:

标签: php regex string


【解决方案1】:

这是一个简单的 1 行解决方案,用于删除部分字符串直到 mp4=:

$string = '[video width="640" height="360" mp4="http://click.ir/wp-content/uploads/2017/09/Spinning_cylinder_shaped_elevator.mp4"][/video]"';
$data = stristr($string, 'mp4="');
var_dump($data);
// string(96) "mp4="http://click.ir/wp-content/uploads/2017/09/Spinning_cylinder_shaped_elevator.mp4"][/video]""

但如果你只想要 url 字符串:

$string = '[video width="640" height="360" mp4="http://click.ir/wp-content/uploads/2017/09/Spinning_cylinder_shaped_elevator.mp4"][/video]"';
preg_match('#"\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))"#', $string, $match);
var_dump(trim($match[0],'"'));
// string(80) "http://click.ir/wp-content/uploads/2017/09/Spinning_cylinder_shaped_elevator.mp4"

【讨论】:

    【解决方案2】:

    要提取 mp4 URL 部分,只需使用preg_match:

    preg_match('#mp4=\"(.*)\"#i', $content, $result);
    if(!empty($result[1])){
        $url = $result[1];
    }else{
       $url = "";
    }
    

    【讨论】:

      【解决方案3】:

      我假设您想要获取 URL。我会使用正则表达式从中获取网址。正则表达式为 http[\S]*.mp4。

      在代码中是:

      $string = '[video width="640" height="360" mp4="http://click.ir/wp-content/uploads/2017/09/Spinning_cylinder_shaped_elevator.mp4"][/video]"';
      // use regex to fetch the url
      preg_match("/http[\S]*\.mp4/", $string,  $matches);
      // get first match
      $url = $matches[0];
      
      // $url is http://click.ir/wp-content/uploads/2017/09/Spinning_cylinder_shaped_elevator.mp4
      

      URL 有更好的正则表达式,但在您的情况下,这会起作用。根据您的传入数据,您可以调整您的正则表达式以获得您想要的。

      参见 preg_match 文档:http://php.net/manual/en/function.preg-match.php

      在线正则表达式测试器:https://www.regexpal.com/

      大量正则表达式信息:http://www.regular-expressions.info/

      【讨论】:

      • 好的,所以如果url 总是以.mp4 结尾,那么这段代码可以工作,但如果在某些情况下不是这样呢?
      • @mega6382,我说过,还有其他方法可以使用正则表达式。我的正则表达式只是完成了工作。您提供的正则表达式要优越得多。这就是为什么我添加了几个关于正则表达式信息的链接,以便人们了解它。
      【解决方案4】:

      使用正则表达式。如果我的问题正确,则需要删除字符串的 url 部分,直到该字符串的最后。这是您可以使用的代码:

      <?php
          $str = '[video width="640" height="360" mp4="http://click.ir/wp-content/uploads/2017/09/Spinning_cylinder_shaped_elevator.mp4"][/video]';
          $str = preg_replace_callback("/(\[video .*mp4=\")(.*)/", 'the_function', $str);
          function the_function($matches) {
              return $matches[1];
          }
          var_dump($str);
      ?>
      

      【讨论】:

      • preg_replace_callback?严重地?用于返回数组的第一个元素?
      • @Codeartist 我的问题中 preg_replace_callback 的输入不是静态的,宽度和高度可能会被调整大小
      • 查看更新,现在它会考虑到数字可能会发生变化。
      • 至于preg_replace_callback,是的,它有点慢,但正如作者所说,字符串内的内容是可变的,我认为这个解决方案更通用。尤其是当事情变得越来越复杂时。正则表达式可以很容易地更改以适应任何需求,如果作者突然需要保留 URL 而不是之前的标记,他只需要更改函数内部的索引。
      • 我的评论不是关于正则表达式,而是关于回调部分;)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多