donglin7979 2014-03-31 06:54
浏览 5
已采纳

将分隔符内的文本转换为有效的URL

I have to convert an old website to a CMS and one of the challenges I have is at present there are over 900 folders that contain up to 9 text files in each folder. I need to combine the up to 9 text files into one and then use that file as the import into the CMS.

The file concatenation and import are working perfectly.

The challenge that I have is parsing some of the text in the text file.

The text file contains a url in the form of

Some text [http://xxxxx.com|About something] some more text

I am converting this with this code

if (substr ($line1, 0, 7) !=="Replace") {
    $pattern = '/\\[/';
    $pattern2 = '/\\]/';
    $pattern3 = '/\\|/';
    $replacement = '<a href="';
    $replacement3 = '">';
    $replacement2='</a><br>';

    $subject = $line1;
    $i=preg_replace($pattern, $replacement, $subject, -1 );
    $i=preg_replace($pattern3, $replacement3, $i, -1 );
    $i=preg_replace($pattern2, $replacement2, $i, -1 );

    $line .= '<div class="'.$folders[$x].'">'.$i.'</div>' ;
}

It may not be the most efficient code but it works and as this is a one off exercise execution time etc is not an issue.

Now to the problem that I cannot seem to code around. Some of the urls in the text files are in this format

Some text [http://xxxx.com] some more text

The pattern matching that I have above finds pattern and pattern2 but as there is no pattern3 the url is malformed in the output.

Regular expressions are not my forte is there a way to modify what I have above or is there another way to get the correctly formatted url in my output or will I need to parse the output a second time looking for the malformed url and correct it before writing it to the output file?

  • 写回答

1条回答 默认 最新

  • doulin2555 2014-03-31 07:00
    关注

    You can use preg_replace_callback() to achieve this:

    • Find any string of the format [...]
    • Try to split them by the delimiter | using explode()
      • If the split array contains two pieces, then it means the [...] string contains two pieces: the link href and the link anchor text
      • If not, then it means the the [...] string contains only the link href part
    • Format and return the link

    Code:

    $input = <<<EOD
    Some text [http://xxxxx.com|About something] some more text
    Some text [http://xxxx.com] some more text
    EOD;
    
    $output = preg_replace_callback('#\[([^\]]+)\]#', function($m)
    {
        $parts = explode('|', $m[1]);
        if (count($parts) == 2)
        {
            return sprintf('<a href="%s">%s</a>', $parts[0], $parts[1]);
        }
        else
        {
            return sprintf('<a href="%1$s">%1$s</a>', $m[1]);
        }
    }, $input);
    
    echo $output;
    

    Output:

    Some text About something some more text
    Some text http://xxxx.com some more text

    Live demo

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

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多