簡體   English   中英

PHP regex 用一些標記替換鏈接

[英]PHP regex to replace links with some tokens

我正在查看一些嵌入了音頻播放鏈接的 HTML,如下所示:

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

我正在嘗試以編程方式用這種格式的令牌替換它:

{a:filename:title}

但有些事情不正常。 有人可以看一下嗎:

<?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;

?>

您可以使用以下正則表達式:

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

捕獲組 1 包含文件名,組 2 包含標題。

我讓它像這樣工作:

$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