doutao6330 2016-06-25 20:05
浏览 118
已采纳

在第一个破折号之前从字符串中提取标题

I have my working code which extracts the title from a string, but right now it still isn't very flexible.

Current code:

$post_title = "THIS IS A TEST - 10-01-2010 - HELLO WORLD (OKAY)!!";
$post_title = substr($post_title, 0, strpos($post_title, '-') - 1);

I want to get the title of the string, which is at the start of the string and goes until the first dash. I don't want to get the spaces before the dash and it could be that there is no dash at all.

The output of the current code works and outputs THIS IS A TEST.

But the current code doesn't work for the following cases, so I need a more flexible code:

  • THIS IS A TEST - 10-01-2010 - HELLO WORLD (OKAY)!!
  • THIS IS A TEST-10-01-2010 - HELLO WORLD (OKAY)!!
  • THIS IS A TEST - - - - 10-01-2010 - HELLO WORLD (OKAY)!!
  • THIS IS A TEST

So the title can exist without a - and someone could forget to put a space between the -, equally, someone could put too many spaces.

The output for all the above cases should always be THIS IS A TEST with no spaces at the end.

With the code I have, the only one that works is the first one.

   $title= explode('-', $post_title);
    $post_title=trim($title[0]);
    $trimmedTitle=$post_title;



    $str = "THIS IS A TEST -0-01-2010 - HELLO WORLD (OKAY)!!";
    preg_match("/^([\w\s]+)\s*-?/m", $str, $m);
    print_r($m);
    var_dump($variableName);



$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS));
$files = array();
$notAllowedExtension = array('jpg', 'vtr', 'html', 'htm' );

foreach($iterator as $file){

if(!in_array($file->getExtension(), $notAllowedExtension) && !$file->isDir())

//$files[filemtime($file->getPathname())] = $file->getPathname();

$files[] = $file->getPathname();

}

sort($files);

echo "<pre>" . print_r($files, true) . "</pre>";
  • 写回答

1条回答 默认 最新

  • dpkpaxhzffixp8426 2016-06-25 23:03
    关注

    Use explode() with the "-" as a delimiter to split it into chunks based on the presence of the "-" and then take the first portion and use trim() to trim the trailing spaces to give the title with no trailing spaces. This gives "THIS IS A TEST" in all the provided test cases. If there are no dashes then the entire string is returned.

    <?php
    
        $str = 'THIS IS A TEST - 10-01-2010 - HELLO WORLD (OKAY)!!';
        $title= explode('-', $str);
        $trimmedTitle=trim($title[0]);
        print_r($trimmedTitle);
    
        //$trimmedTitle ='THIS IS A TEST 
    
    ?>
    

    I have tested this against:

    THIS IS A TEST - 10-01-2010 - HELLO WORLD (OKAY)!!
    THIS IS A TEST-10-01-2010 - HELLO WORLD (OKAY)!!
    THIS IS A TEST - - - - 10-01-2010 - HELLO WORLD (OKAY)!!
    THIS IS A TEST
    

    and each returns THIS IS A TEST with no trailing spaces

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题