简体   繁体   English

PHP regex 用一些标记替换链接

[英]PHP regex to replace links with some tokens

I'm looking at some HTML that has embedded audio-playback links like that:我正在查看一些嵌入了音频播放链接的 HTML,如下所示:

...<input type="button" onclick="return play('filename', 'title');" class="playlink" />...

I'm trying to programmatically replace that with tokens in this format:我正在尝试以编程方式用这种格式的令牌替换它:

{a:filename:title}

But something's not working right.但有些事情不正常。 Could someone look at it:有人可以看一下吗:

<?php

$src = 'aaaaa<input type="button" onclick="return play(\'hellofile\', \'hellotitle\');" class="playlink" />bbbbb';
$t = preg_replace('#<input\ type="button"\ onclick="return play(\'(.*?)\',\ \'(.*?)\');"\ class="playlink"\ />#us', '{a:${1}:${2}}', $src);
echo $t;

?>

You can use the following regex:您可以使用以下正则表达式:

<input.*?play\\('(\\w+?)',\\s*?'(\\w+?)'.*?\\/>
Demo here .演示在这里

Capture group 1 contains filename, group 2 contains the title.捕获组 1 包含文件名,组 2 包含标题。

I got it to work like this:我让它像这样工作:

$src = 'aaaaa<input type="button" onclick="return play(\'hellofile\', \'hellotitle\');" class="playlink" />bbbbb';
$rgx = '#<input type="button" onclick="return play\(' . "'(.*?)', '(.*?)'" . '\);" class="playlink" />#u';
$t = preg_replace($rgx, '{a:${1}:${2}}', $src);
echo "$t\n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM