简体   繁体   中英

PHP regex to replace links with some tokens

I'm looking at some HTML that has embedded audio-playback links like that:

...<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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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