开发者

Regex to replace 'li' with 'option' without losing class and id attributes

I am looking for a solution using preg_replace or similar method to change:

<li id="id1" class="authorlist" />
<li id="id2" class="authorlist" />
<li id="id3" class="authorlist" />

to

<option id="id1" class="authorlist" />
<option id="id2" class="authorlist" />
<option id="id3" class="authorlist" />

开发者_如何学CI think I have the pattern correct, but not sure how to do the replacement part...

Here is the (wordpress) php code:

$string = wp_list_authors( $args ); //returns list as noted above
$pattern = '<li ([^>]*)';
$replacement = '?????';
echo preg_replace($pattern, $replacement, $string);

Suggestions, please?


I think a simple string replacement would be best

str_replace('<li', '<option', $string)

Same for ending tags

str_replace('</li', '</option', $string)


Like this:

echo str_replace('</li', '</option', (str_replace('<li', '<option', $string));


You need a delimiter in the pattern. For example:

$pattern = '@<li @';
$replacement = '<option ';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜