以下是一个PHP字幕格式的实例,我们将通过表格形式展示如何使用PHP创建字幕,并在HTML中显示它们。
| 步骤 | PHP代码示例 | HTML代码示例 |
| --- | --- | --- |
| 1 | 创建字幕数据 | 在PHP脚本中创建字幕数组: |
```php
$speeches = [
'00:00:01,000 --> 00:00:03,000',
'00:00:03,000 --> 00:00:06,000',
'This is the first subtitle.',
'00:00:06,000 --> 00:00:09,000',
'00:00:09,000 --> 00:00:12,000',
'This is the second subtitle.'
];
>
```
| 2 | 显示字幕 | 使用PHP循环遍历数组,并在HTML中显示字幕: |
```php
foreach ($speeches as $speech) {
// 使用正则表达式分离时间戳和文本
preg_match('/^(.*),(.*) --> (.*),(.*)$/', $speech, $matches);
$start = $matches[1];
$end = $matches[4];
$text = substr($matches[5], 1); // 去掉开头的大括号
// 在HTML中显示字幕
echo "