Earthman Web
Earthman Web

Reputation: 1

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

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

Upvotes: 0

Views: 735

Answers (3)

Johan
Johan

Reputation: 5053

You need a delimiter in the pattern. For example:

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

Upvotes: 0

Syntax Error
Syntax Error

Reputation: 4527

I think a simple string replacement would be best

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

Same for ending tags

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

Upvotes: 6

SLaks
SLaks

Reputation: 887469

Like this:

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

Upvotes: 2

Related Questions