Solved

Preg_replace on mutliple <li>

Posted on 2011-03-16
13
540 Views
Last Modified: 2012-06-21
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
Comment
Question by:handypam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35152103
Presumably you want to keep counting up when there are more than 2 list items?
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35152108
(Can you specify a maximum number?)
0
 
LVL 12

Expert Comment

by:tel2
ID: 35152118
Hi handypam,

Is this the kind of thing you're after:
    s/^<li>(.*?)<\/li>$/<li style="$1">$1<\/li>/
0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 12

Expert Comment

by:tel2
ID: 35152121
But my solution above just replaces one line at a time, of course.
0
 

Author Comment

by:handypam
ID: 35152175
HI there,
yes there will be any number of <li>'s this was my problem which i couldnt solve.
:(
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35152235
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
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35152335
Looks perfect kaufmed... if my brain was working properly today I should have suggested that first off....
0
 
LVL 12

Expert Comment

by:tel2
ID: 35152856
Hi handypam,

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

Expert Comment

by:käµfm³d 👽
ID: 35153099
@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
 
LVL 12

Expert Comment

by:tel2
ID: 35153138
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35153292
preg_replace() does global replacement by default unless you specify the number of replacements to occur     = )
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35153301
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
 
LVL 12

Expert Comment

by:tel2
ID: 35153560
Points taken, kaufmed.

Keep up the good work.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question