PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Wed Nov 26, 2014 5:46 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Sat Feb 18, 2012 2:02 pm 
Offline
Forum Newbie

Joined: Sat Feb 18, 2012 1:59 pm
Posts: 1
Hi I want to replace first three "Next" words to "Previous" as I shown below, I tried some tools but I couldn't what regexes should I use to do this?

Code:
               <li style="margin-right: 20px;">
                        <a href="4.html" title="Next">
   <img src="../thumbs/4.JPG" alt="Next" />
   <h2>Next</h2>
   <div class="clear"></div>
</a>

               </li>
               <li style="margin-left: 20px;">
                        <a href="6.html" title="Next">
   <img src="../thumbs/6.JPG" alt="Next" />
   <h2>Next</h2>




to this:

Code:
                  <li style="margin-right: 20px;">
                        <a href="4.html" title="Previous">
   <img src="../thumbs/4.JPG" alt="Previous" />
   <h2>Previous</h2>
   <div class="clear"></div>
</a>

               </li>
               <li style="margin-left: 20px;">
                        <a href="6.html" title="Next">
   <img src="../thumbs/6.JPG" alt="Next" />
   <h2>Next</h2>


Top
 Profile  
 
PostPosted: Sat Feb 18, 2012 2:38 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
Hi Seamoon!

1. Run this:

Code:
Code:
<?php
$string='<li style="margin-right: 20px;">
                        <a href="4.html" title="Next">
   <img src="../thumbs/4.JPG" alt="Next" />
   <h2>Next</h2>
   <div class="clear"></div>
</a>

               </li>
               <li style="margin-left: 20px;">
                        <a href="6.html" title="Next">
   <img src="../thumbs/6.JPG" alt="Next" />
   <h2>Next</h2>';
$regex=',(?s)^(?>.*?\b\K(Next))\b(?>(.*?)\b(Next))\b(.*?)\bNext\b,';
$replace='Previous\2Previous\4Previous';
$string=preg_replace($regex,$replace,$string,1);
echo htmlentities($string);
?>


Output:
Syntax: [ Download ] [ Hide ]
<li style="margin-right: 20px;"> <a href="4.html" title="Previous"> <img src="../thumbs/4.JPG" alt="Previous" /> <h2>Previous</h2> <div class="clear"></div> </a> </li> <li style="margin-left: 20px;"> <a href="6.html" title="Next"> <img src="../thumbs/6.JPG" alt="Next" /> <h2>Next</h2>


2. Please don't cross-post.

Hope this works for you, let me know if you have any questions. :)

Wishing you a fun weekend.


Top
 Profile  
 
PostPosted: Sat Feb 18, 2012 5:54 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
Hi again,
I know from the other post that your problem is now solved.
But in hindsight and for the record for the php solution: now that I'm awake, I see that I could have made the solution much simpler by using "3" as the fourth argument in the preg_replace, so that we simply replace Next three times.
Syntax: [ Download ] [ Hide ]
$string=preg_replace(',\bNext\b,','Previous',$string,3);

It's one of those times when I jump to the complicated.
:)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group