Solved

Regular expression woes

Posted on 2002-04-20
2
146 Views
Last Modified: 2008-03-06
I want to find every occurance of a string and be able to replace it with a different string depending on a parameter passed.  (Cryptic, I know)

<mytag=some parameter>stuff inside!</mytag> text inbetween <mytag=param again>more stuff even</mytag>

The problem I am having is that my code will give me "stuff inside!</mytag> text inbetween <mytag=param again>more stuff even" instead of each one seperately.

Here is my code:

$regexp="\[mytag\=([a-zA-Z0-9 ]+)\]([[:graph:] ]+)\[\/mytag\]";
while(eregi($regexp,$message,$regs)){
  if(str_replace(" ","",strtolower("Tom Apps com"))==str_replace(" ","",strtolower($regs[1]))){
    $message=eregi_replace($regexp,"\\2",$message);
  }else{
    $message=eregi_replace($regexp,"",$message);
  }
}
echo $message;

Help if you can! :)
0
Comment
Question by:VBTom
2 Comments
 
LVL 6

Accepted Solution

by:
andreif earned 100 total points
Comment Utility
Problem is that program looks for the longest match.
To make it look for the shortest, add ? to * or +

Your RegExp:

$regexp="\[mytag\=([a-zA-Z0-9 ]+)\]([[:graph:] ]+)\[\/mytag\]";

should read as

$regexp="\[mytag\=([a-zA-Z0-9 ]+)\]([[:graph:] ]+?)\[\/mytag\]";
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now