【问题标题】:How to Format text in php [closed]如何在php中格式化文本[关闭]
【发布时间】:2012-12-19 10:47:17
【问题描述】:

我在pdftotext的帮助下从pdf文件生成文本
我的问题不在于 pdftotext,而在于相应地格式化文本

Salman              Madhuri             Mohnish             Renuka                Anupam
Khan                Dixit               Behl                Shahane               Kher
Prem                Nisha Chou...       Rajesh              Pooja Chou...         Prof. Siddh


Hum Aapke Hain Koun...! (1994) - Full cast and crew
www.imdb.com/title/tt0110076/fullcredits
Hum Aapke Hain Koun...! on IMDb: Movies, TV, Celebs, and more... ... IMDbPro.com
offers representation listings for over 120,000 individuals, including actors, ...

我需要输出为

Salman Khan Prem
Madhuri Dixit Nisha Chou...
Mohnish Behl Rajesh
Renuka Shahane Pooja Chou...
Anupam Kher Prof.

Hum Aapke Hain Koun...! (1994) - Full cast and crew
www.imdb.com/title/tt0110076/fullcredits
Hum Aapke Hain Koun...! on IMDb: Movies, TV, Celebs, and more... ... IMDbPro.com
offers representation listings for over 120,000 individuals, including actors, ...

【问题讨论】:

  • 分隔符是什么?
  • 向我们展示一些产生此结果的代码。

标签: php regex text-formatting


【解决方案1】:

不确定你的分隔符是什么,但你可以像下面这样(有点难看,但它可以完成工作):

$namesAndContent = explode("\r\n\r\n", $theString);
$nameRows = explode("\r\n", $namesAndContent[0]);
$names = array();
foreach ($nameRows as $row) {
    $items = preg_split('/\s{2,}/', $row);
    foreach ($items as $index => $namePart) {
        if (!array_key_exists($index, $names)) {
            $names[$index] = array();
        }
        $names[$index][] = $namePart;
    }

}

foreach ($names as $name) {
    echo implode(' ', $name) . "\r\n";
}
echo "\r\n";
echo $namesAndContent[1];

演示:http://codepad.viper-7.com/Nr1Q4t

上面会格式化数据(当分隔符正确时),但我想知道数据来自哪里(原始而不是 pdf),因为我怀疑有更好的方法来解决你的问题.也许有一些 API 你可以直接使用

【讨论】:

  • 如果其他文本在字符串中更多\n\n,则无法解决...您能否建议如何解决...。
  • 其他文字是什么,的分隔符是什么。
  • 感谢您的帮助....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-08
  • 2016-12-27
  • 2012-05-29
  • 2021-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多