• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 551
  • Last Modified:

Preg_replace on mutliple <li>

Hello experts

Does anyone know how to achieve the following?

<li>one</li>
<li>two></li>

Perform a preg_replace on them so that it becomes
<li style="one">one</li>
<li style="two">two</li>

Thanks in advance
:)
0
handypam
Asked:
handypam
  • 5
  • 4
  • 3
  • +1
1 Solution
 
Terry WoodsIT GuruCommented:
Presumably you want to keep counting up when there are more than 2 list items?
0
 
Terry WoodsIT GuruCommented:
(Can you specify a maximum number?)
0
 
tel2Commented:
Hi handypam,

Is this the kind of thing you're after:
    s/^<li>(.*?)<\/li>$/<li style="$1">$1<\/li>/
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
tel2Commented:
But my solution above just replaces one line at a time, of course.
0
 
handypamAuthor Commented:
HI there,
yes there will be any number of <li>'s this was my problem which i couldnt solve.
:(
0
 
käµfm³d 👽Commented:
If the structure of your data is truly as you posted (meaning "one" will be found and used as the replacement value), then you should be able to use the following:
$result = preg_replace('#<li>([^<]*)</li>#', "<li style=\"$1\">$1</li>", $source);

Open in new window

0
 
Terry WoodsIT GuruCommented:
Looks perfect kaufmed... if my brain was working properly today I should have suggested that first off....
0
 
tel2Commented:
Hi handypam,

So were you happy to have a solution that processes just one line per call?
0
 
käµfm³d 👽Commented:
@tel2

>>  So were you happy to have a solution that processes just one line per call?

You are clear on which language we're working in here, correct? I mean you did, after all, post a PERL solution  ; )
0
 
tel2Commented:
Yes kaufmed, I understood that.  I don't know PHP, but thought handypam may have been able to make use of the regex I supplied.  I'm not complaining that I missed out on points, but it looks to me as if your solution processes one line per call (is that right?), and I'm making sure that handypam understands that (if it is correct), and I'm asking if that meets the requirements.  The subject of the question (see the word "multiple") makes me sceptical.
0
 
käµfm³d 👽Commented:
preg_replace() does global replacement by default unless you specify the number of replacements to occur     = )
0
 
käµfm³d 👽Commented:
P.S.

I'm not saying there is anything wrong with the pattern you supplied, but some people aren't accustomed to the PERL replacement syntax ( s/.../.../g ). Regex in and of itself is complicated enough for many people and I think not posting the appropriate syntax for the target language can cause further confusion  : )
0
 
tel2Commented:
Points taken, kaufmed.

Keep up the good work.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 5
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now